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


How do call Javascript function from a input form in php, to change 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>

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