dqba94619 2014-05-24 06:57
浏览 43
已采纳

在PHP中通过Javascript函数提交

So I have a form.php with 3 input:a(textbox), b(textbox), c(textbox) Can someone tell me what's this sentence mean and what code should I write to make this happen?

"c contain the 1st letter of a and b, c is generated on page submit by a javascript function"

^ Can someone give me an simpler, clearer expression for this sentence?

I can write for the "1st letter" part but I'm not sure about the trigger when press submit and that 2nd phrase. Should I

_write javascript in the php? (which doesn't work, I keep getting error, even when I use echo)

or

_have a javascript.js like a External css? (never try before)

Here's my code:

<?php
$result= 0;
if(isset($_REQUEST["submit"]))
{
  if(isset($_POST["submit"]))
  {
    $result= $_POST["c"];
}}
?>  
<form id="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method= "POST">
<p>
a <input type="text" name= "a" value="Alex" />
b <input type="text" name= "b" value="Phan" />
c <input type="text" name= "c" value= "<?php echo $result ?>" />
<input type="submit" name= "submit" onclick= "getc();" />
</p>
<script type="text/javascript">
function getc()
{
 var firsta = document.getElementsByName("a")[0].value.charAt(0);
 var firstb = document.getElementsByName("b")[0].value.charAt(0);
 var c;
 document.getElementsByName("c")[0].value= firsta + firstb;
}
</script>
</form>

(This code is an update for the answer)

Turn out the javascript didn't work because I tested it on browser, it worked after I uploaded it on the database. Also it doesn't work when I put it like .... ?> either.

  • 写回答

1条回答 默认 最新

  • dousao9569 2014-05-24 07:12
    关注

    you must use document.getElementsByName("a")[0].value.charAt(0) instead of document.getElementByName("a").value.charAt(0) the reason is multiple elements can have the same name.If your elements are unique, give them all unique ID attributes and use getElementById

    here is the working code

    <html>
    <script type="text/javascript">
    function getc(obj)
    {
        var firsta = document.getElementsByName("a")[0].value.charAt(0);
    
      var firstb = document.getElementsByName("b")[0].value.charAt(0);
      var c;
      obj.value= firsta + firstb;
    }
    </script>
    <form id="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method= "POST">
    <p>
      a <input type="text"  name= "a" value="Alex" />
      b <input type="text"  name= "b" value="Phan" />
      c <input type="text"  name= "c" onfocus= "getc(this);"/>
    </p>
    </html>
    

    If you want the result on submitting the form then change

    c <input type="text"  name= "c" onfocus= "getc(this);"/>
    

    To

    c <input type="text"  name= "c"/>
     <input type="submit"  name= "submit" onclick="getc();" value="submit"/>
    

    and in js

    change

    obj.value= firsta + firstb;
    

    to

    document.getElementsByName("c")[0].value=firsta + firstb;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波