当使用javascript调用php函数时,Php $ _POST返回空

I am still studying php and java script. I am creating a simple contact form and set the form action to the same page using $_Server[php_self]

What I want to do is when someone submit to my form, it will show a message including the name that was submitted on the same page. replace the contact form with the message.

I also tried pointing action to a different php page. and it still did not work. Does Javascript work like that? or I have to use different code or language to do that.

Here is my code

<!DOCTYPE html>
<html>
<head>
    <?php 
    include 'action.php';
?>
    <title> My profle</title>
    <meta name="robots" content="noindex">
    <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
<div class="contact">
   <form class="form" class="contact" id="contact-form" action="action.php" method="POST">
            Name: <br>
           <input type="text" class="field" name="name"><br>
           Number:<br>
           <input type="text" class="field" name="number"><br>
           Email:<br>
           <input type="email" class="field" name="email:>"<br>
           <br>
           <input type="submit" class="submit" name="submit" value="submit"
           onclick ="document.getElementById('contact-form').innerHTML='<?php thankyou();?>'">
       </form>
</div>
</body>
</html>

Then here is the action.php

<?php
    function thankyou(){
        $name = $_POST['name'];
        echo "Thank you"." $name ! Your message has been submitted.";
    }
?>

</div>
dongzuo4666
dongzuo4666 对不起,误解了评论
2 年多之前 回复
donglin4636
donglin4636 这不会像想要的那样工作,你将不得不使用js来获取感谢信息的名称,然后提交表单以便在php中处理
2 年多之前 回复
doutun9179
doutun9179 你可以用这种方式混合js/php。问题是,在提交表单之前尚未设置$_POST。第二次提交表单时,应显示名称。
2 年多之前 回复
dongzhangnong2063
dongzhangnong2063 附:javascript返回thankyou()函数成功但$name缺失/
2 年多之前 回复

2个回答



这里有几个不同的问题。</ p>

首先是缺乏理解 PHP和JS运行的时间。</ p>

第二个是加载新页面时DOM更改会丢失。</ p>

这是什么 发生:</ p>


  1. 浏览器请求页面</ li>
  2. PHP运行( $ _ POST </ code>没有< 代码>名称</ code>密钥)</ li>
  3. 浏览器获取页面</ li>
  4. 单击提交按钮</ li>
  5. JavaScript运行, 并设置 innerHTML </ code>(请记住PHP在步骤2中运行)</ li>
  6. 表单提交导致浏览器导航到新页面</ li>
    < li> PHP再次运行</ li>
  7. 浏览器获取新页面...并且前一页的DOM更改将丢失</ li>
    </ ol>

    进一步阅读:有什么区别 客户端和服务器端编程?。</ p>
    </ div>

展开原文

原文

You have a couple of different problems here.

The first is a lack of understanding about the timing of when PHP and JS run.

The second is that DOM changes are lost when a new page is loaded.

This is what is happening:

  1. The browser requests the page
  2. The PHP runs ($_POST does not have a name key)
  3. The browser gets the page
  4. You click the submit button
  5. The JavaScript runs, and set the innerHTML (remember that the PHP ran back at step 2)
  6. The form submits causing the browser to navigate to a new page
  7. The PHP runs again
  8. The browser gets the new page … and the DOM changes make to the previous page are lost

Further reading: What is the difference between client-side and server-side programming?.

donglvmang8638
donglvmang8638 +1以信息方式回答仍然导致一些研究而不是复制和粘贴代码。 试着让我的答案更像这样......
2 年多之前 回复



我愿意在评论中听到更好的方法来做到这一点,因为这是我的头脑,我知道那里 是更好的解决方案...但这是我在你的情况下会做的事情:</ p>

 &lt;!DOCTYPE html&gt; 
&lt; html&gt;
&lt; head&gt;
&lt; 标题&GT; 我的个人资料&lt; / title&gt;
&lt; meta name =“robots”content =“noindex”&gt;
&lt; link rel =“stylesheet”type =“text / css”href =“style.css”&gt; \ n
&lt; / head&gt;
&lt; body&gt;
&lt; div class =“contact”&gt;
&lt; form class =“form”class =“contact”id =“contact-form”action =“”method =“POST”&gt;
&lt;?php
if(isset($ _ POST)){
// //然后处理数据(我假设)你需要 - 即使此时只是发布到另一个脚本 ...
echo'谢谢'。$ _ POST ['name']。' ! 您的邮件已提交';
}
其他{
echo'Name:&lt; br&gt;
&lt; input type =“text”class =“field”name =“name”&gt;&lt; br&gt;

编号:&lt; br&gt;
&lt; input type =“text”class =“field”name =“number”&gt;&lt; br&gt;
电子邮件:&lt; br&gt;
&lt; input type =“ 电子邮件“class =”字段“name =”email“&gt;&lt; br&gt;
&lt; br&gt;
&lt; input type =”submit“class =”submit“name =”submit“value =”submit“&gt ;';
}
?&gt;
&lt; / form&gt;
&lt; / div&gt;
&lt; / body&gt;
&lt; / html&gt;
</ code> </ pre>
</ DIV>

展开原文

原文

I'm open to hearing better ways to do this in the comments, as this is off the top of my head and I know there are better solutions... but here's what I would do in your situation:

<!DOCTYPE html>
<html>
<head>
    <title> My profle</title>
    <meta name="robots" content="noindex">
    <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
<div class="contact">
   <form class="form" class="contact" id="contact-form" action="" method="POST">
           <?php 
               if(isset($_POST)){
                   //process data however (I'm assuming) you need to - even if just posting to another script at this point...
                   echo 'Thank you '.$_POST['name'].' ! Your message has been submitted';
               }
               else{
                    echo 'Name: <br>
                    <input type="text" class="field" name="name"><br>
                    Number:<br>
                    <input type="text" class="field" name="number"><br>
                    Email:<br>
                    <input type="email" class="field" name="email"><br>
                    <br>
                    <input type="submit" class="submit" name="submit" value="submit">';
               }
           ?>
       </form>
</div>
</body>
</html>

doubi9615
doubi9615 np,认为可能有助于看到一些正确考虑语言处理顺序的代码 - 虽然我希望有更多的社区输入 - 我知道使用php和html一起有很多最佳实践,有些人不同意什么是 实际上是最佳实践
2 年多之前 回复
dsfovbm931034814
dsfovbm931034814 谢谢,我还需要学习更多,我会看看如何正确组合和计时。 无论如何,这是我一直在努力的演示。 resume.sofisticadoblends.com
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问