Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax php提交表单_Php_Ajax_Forms - Fatal编程技术网

使用ajax php提交表单

使用ajax php提交表单,php,ajax,forms,Php,Ajax,Forms,使用ajax提交简单的联系人表单 编辑的代码 现在正在尝试将数据插入数据库 html表单 <form align="left" method="post" class="subscribe_form" action='subscribe.php'> Your Name:<br> <input type="text" name="name" value="" required><br>

使用ajax提交简单的联系人表单

编辑的代码 现在正在尝试将数据插入数据库

html表单

        <form align="left" method="post" class="subscribe_form" action='subscribe.php'> 

                    Your Name:<br>
  <input type="text" name="name" value="" required><br>

Your E-Mail:<br>
  <input type="email" name="email" value="" required><br><br>

  Gender:
  <p>    <input name="gender" value="male" type="radio" id="male" />
      <label for="male">Male</label>

      <input name="gender" value="female" type="radio" id="female" />
      <label for="female">Female</label>
    </p>
<br>

Company Name:
<input type="text" name="cname" value="" required><br><br>
<input type="submit" name="send" value="Subscribe" id="subscribe"> <span class="output_result"></span>
</form>
此代码给我“错误消息”

如果我在没有ajax代码的情况下使用

在形式上

没有“class=”订阅表单“

在subscribe.php中

 if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
} 
它很好用。”已成功创建新记录并将“数据”插入表中


请帮助我学习ajax代码。我不熟悉ajax。如何使用ajax实现这一点

我认为您需要将数据类型添加到ajax请求中。实际上,您的ajax请求需要json,您返回的文本是“Success”,这不是json,这就是为什么当它返回意外数据时,它会向error函数发送ajax响应。根据以下ajax请求更新您的代码-

当您返回文本作为对ajax请求的响应时,您的ajax请求数据类型必须是ajax请求中代码行下方的文本-

dataType: "text"
预期Ajax请求的示例

 $.ajax({
            type:'post',
            url:'email.php',
            dataType: "text",
            data: form.serialize(),
            success: function(result){
                if (result == 'success'){
                    $('.output_message').text('Message Sent Successfully!');  
                } else {
                    $('.output_message').text('Error Sending email!');
                }
            }
        });

        // Prevents default submission of the form after clicking on the submit button. 
        return false;   
    });

您是否可以同时显示您的email.php文件以查看它返回到ajax请求的内容。$send_email=mail($to,$subject,$message,$headers);echo($send_email)?'成功':'错误';您可以尝试将数据类型添加到ajax请求中吗。我认为ajax请求在从email.php返回时无法识别数据的类型。尝试在data:form.serialize()这一行之后向ajax请求添加-datatype:“text”,我认为ajax期望json作为结果数据,而您返回的“Success”不是json。请检查这是否对你有帮助。但我首先有一个疑问,email.php是否在呼叫?!echo$name;echo$电子邮件;不打印任何内容。但是当我在email.php中使用这个“”而不使用脚本时--echo$name;echo$电子邮件;显示姓名和电子邮件。
$result = (mysqli_query($conn, $sql));
echo ($result) ? 'success' : 'error'; 
dataType: "text"
 $.ajax({
            type:'post',
            url:'email.php',
            dataType: "text",
            data: form.serialize(),
            success: function(result){
                if (result == 'success'){
                    $('.output_message').text('Message Sent Successfully!');  
                } else {
                    $('.output_message').text('Error Sending email!');
                }
            }
        });

        // Prevents default submission of the form after clicking on the submit button. 
        return false;   
    });