简体   繁体   中英

Printing text on the webpage using PHP/AJAX/HTML

I have the following simple Form

<form action="" method="POST">
<input type="button" value="Generate Numbers" onclick="on_callPhp1()"/>
</form>

And this javascript

<script type="text/javascript">
function on_callPhp()
{
    var result = "<?php php_func1();?>";
    alert(result);
    return false;
}
</script>

and this PHP Script

function php_func1()
{
    echo "Hello PHP";
}
?>

The above works perfectly, and whenever I press the button, I can see the Alert of the PHP. However, I do not want the alert to be seen, instead, I want the Hello PHP text be written on the page. For that I tried, to remove the alert(result); and instead put echo (result); but it did not work and nothing shows.

I want the text to appear in the body of the page.

Mainly, I will be putting a for loop and generate random numbers inside php_func1() and I want them to appear under the buttons. e

Any ideas?

What you're immediately looking to do is as simple as the following I suppose:

<form action="" method="POST">
  <input type="button" value="Generate Numbers" onclick="on_callPhp()"/>
</form>
<div id="phptext"></div>

and

<script type="text/javascript">
  function on_callPhp()
  {
      var result = "<?php php_func1();?>";

      document.getElementById('phptext').innerHTML = result;
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM