简体   繁体   English

Ajax不会向php表单发起POST请求

[英]Ajax won't initiate POST request to php form

For some reason, my form won't post, any ideas as to why(I already know that everything else on my server is operational)? 出于某种原因,我的表单无法发布,为什么没有任何想法(我已经知道服务器上的所有其他功能都可以运行)? Code below: 代码如下:

PHP PHP

    <?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "<br>";
$ret = file_put_contents('user.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
}
?>

https://pastebin.com/hHeMD4Mq https://pastebin.com/hHeMD4Mq


HTML/AJAX JS HTML / AJAX JS


<form id ="form">
<p>Name:</p>
<input name="field1" type="text" id ="name" />
<h3>&nbsp;</h3>
<p>Message:</p>
<textarea name="field2" type="text" id ="message"></textarea><br/>
<button onclick="pos();">reply</button>
</form>
</td>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function pos(){
var values=$("form").serialize();
$.post('form.php', values, function(response) {
    console.log("Response: "+response);
});}
</script>

https://pastebin.com/eAVE8EGS https://pastebin.com/eAVE8EGS


demo on my site: 在我的网站上进行演示:

jakesandbox.com/dnd/ jakesandbox.com/dnd/

(any info not provided above will be provided upon request in the comments) (以上未提供的任何信息将根据要求在评论中提供)

The problem is your button is actually submitting the form (Which is essentially reloading the page, as it's submitting to itself). 问题在于您的按钮实际上是在提交表单(实际上是在重载页面,因为它是在向自身提交)。 You need to do one of 2 things: 您需要执行以下两项操作之一:

  1. Change your button to a type="button" to prevent it from submitting (A button inside of a form element automatically becomes a submit button): 将您的按钮更改为type="button"以防止其提交(表单元素内部的按钮会自动变为提交按钮):
<button type="button" onclick="pos();">reply</button>
  1. Prevent the click action from taking place (Thus preventing the submit): 阻止点击操作的发生(从而阻止提交):
<button type="button" onclick="pos(event);">reply</button>
<script type="text/javascript">
function pos(e){
e.preventDefault();
var values=$("form").serialize();
$.post('form.php', values, function(response) {
    console.log("Response: "+response);
});}
</script>

-OR- -要么-

<button type="button" onclick="pos(); return false;">reply</button>

It turns out your form is actually sent normally with GET request (you can probably see the page reload). 事实证明,您的表单实际上是通过GET请求正常发送的(您可能会看到页面重新加载)。 It occurs when a button inside a form has no specified type (default being submit). 当表单内的按钮没有指定的类型(默认为提交)时,就会发生这种情况。

A button of type "submit" inside a form submits the form while a button of type "button" doesn't. 表单内“提交”类型的按钮提交表单,而“按钮”类型的按钮则不提交。

Just add the attribute type="button" to your button and your problem should be solved. 只需将属性type =“ button”添加到您的按钮即可解决您的问题。

<button type="button" onclick="pos();">reply</button>

On your php end to debug if the data actually came 在您的php端上调试是否确实有数据

<?php
header('Content-Type: application/json');
echo json_encode($_POST);

Then on your js 然后在你的js上

$.post('form.php', values, function(response) {
   console.log(response); // return json
});}

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

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