如何从php中的输入表单调用Javascript函数来更改HTML

时间:2013-11-30 22:46:53

标签: javascript php html

我正在尝试创建一个表单,在提交后,将显示第一个名称,选择的心情,用户的经度和纬度。当我点击输入表单时,我没有运气调用javascript函数,我已经尝试将Javascript放入/放出PhP,我也将代码段放在不同的顺序。我究竟做错了什么?

    <!DOCTYPE html>
    <head>
    </head>
    <body>

  <h3>PHP HTML Form radio button Example</h3>   
  <form name="UserInformationForm" method="POST" action="#">
  Enter Your Full Name :
  <input name="FullName" type="text" value="<?php echo $_POST['FullName']; ?>"><br/><br/>
  You are :
  <input name="feelings" type="radio" value=" happy" <?php if($_POST['feelings']=="happy") echo "checked=checked"; ?> > Happy
  <input name="feelings" type="radio" value=" sad" <?php if($_POST['feelings']=="sad") echo "checked=checked"; ?> > Sad 
  <input name="feelings" type="radio" value=" busy" <?php if($_POST['feelings']=="busy") echo "checked=checked"; ?> > Busy
  <input name="feelings" type="radio" value=" excited" <?php if($_POST['feelings']=="excited") echo "checked=checked"; ?>> Excited
  <br/><br/>
  <input type="submit" name="BtnSubmit" onclick="getLocation()"></input>
  </form>

  <?php

  if(isset($_POST['BtnSubmit']))
  {
  echo "<h3>Your form data as bellow</h3>";
  echo "<p>Your Name: {$_POST['FullName']} </p>";
  echo "<p>Your are: {$_POST['feelings']} </p>";
  echo "<p>at: </p>" . "<p id=\"demo\">" . "Here</p>";
  echo '<script type="text/javascript">
        var x=document.getElementById("demo");
        function getLocation(){
        if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition);
        }
        else{x.innerHTML="Geolocation is not supported by this browser.";  }
        }
        function showPosition(position) {
        x.innerHTML="Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;  
        } </script>';

    }

    ?>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

我建议将您的javascript放在页面的head部分而不是php中。 在表单提交后,我会将我的地理位置信息附加到dom元素。