简体   繁体   English

使用PHP / AJAX / HTML在网页上打印文本

[英]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 而这个JavaScript

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

and this PHP Script 和这个PHP脚本

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

The above works perfectly, and whenever I press the button, I can see the Alert of the PHP. 上面的方法很完美,每当我按下按钮时,我都可以看到PHP的Alert。 However, I do not want the alert to be seen, instead, I want the Hello PHP text be written on the page. 但是,我不希望看到警报,而是希望将Hello PHP文本写在页面上。 For that I tried, to remove the alert(result); 为此,我尝试删除alert(result); and instead put echo (result); 而是放回声echo (result); but it did not work and nothing shows. 但是它没有用,什么也没显示。

I want the text to appear in the body of the page. 我希望文本显示在页面body中。

Mainly, I will be putting a for loop and generate random numbers inside php_func1() and I want them to appear under the buttons. 主要是,我将放置一个for循环并在php_func1()内生成随机数,我希望它们出现在按钮下方。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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