WordPress ajax表单提交可能不使用wordpress功能吗?
问题描述:
I\'m currently making a site on WordPress and need a form to be submitted via ajax is it possible to do this without using WordPress functions? My current code has no errors and returns a success message without updating the database. I don\'t understand why it\'s not working please have a look at my simplified version -
This is the form HTML -
1 2 3 4 5 6 7 |
<form action=\"\" method=\"post\" id=\"formAppointment\" name=\"appointmentform\"> <input type=\"text\" name=\"message_first_name\" value=\"\" placeholder=\"First name\" id=\"appointmentFirstName\"> <input type=\"text\" name=\"message_last_name\" value=\"\" placeholder=\"Last name\" id=\"appointmentLastName\"> <input type=\"tel\" name=\"message_phone\" value=\"\" placeholder=\"Phone\" id=\"appointmentPhone\"> <input type=\"submit\" id=\'appointmentSubmit\' class=\'xAnim\' name=\"submit\"> </form> |
This is the jquery AJAX -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
$(\"#formAppointment\").submit(function(e){ var firstname = $(\"#appointmentFirstName\").val(); var lastname = $(\'#appointmentLastName\').val(); var phone = $(\'#appointmentPhone\').val(); var dataString = \'message_first_name=\'+ firstname + \'&message_last_name=\' + lastname + \'&message_phone=\' + phone; if(firstname.trim() == \"\" || lastname.trim() == \"\" || phone.trim() == \"\"){ alert(\'missing information\'); e.preventDefault(); } else { // AJAX Code To submit Form. $.ajax({ type: \"POST\", url: \"process.php\", data: dataString, cache: false, success: function(result){ console.log(dataString); alert(\'success\'); } }); } return false; }); |
This is the php located in process.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
include \"config.php\"; $patientfirstname = htmlspecialchars($_POST[\'message_first_name\']); $patientlastname = htmlspecialchars($_POST[\'message_last_name\']); $patientcontactnumber = htmlspecialchars($_POST[\'message_phone\']); $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die(\"Connection failed: \" . mysqli_connect_error()); } $sql = \"INSERT INTO data_table (firstname, lastname, phonenumber ) VALUES (\'$patientfirstname\', \'$patientlastname\', \'$patientcontactnumber\')\"; if (mysqli_query($conn, $sql)) { echo \"New record created successfully\"; } else { echo \"Error: \" . $sql . \"<br>\" . mysqli_error($conn); } mysqli_close($conn); |
网友观点:
You have to pass data as object, not as dataString.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$(\\\"#formAppointment\\\").submit(function(e) { e.preventDefault(); var firstname = $(\\\"#appointmentFirstName\\\").val(); var lastname = $(\'#appointmentLastName\').val(); var phone = $(\'#appointmentPhone\').val(); // var dataString = \'message_first_name=\' + firstname + \'&message_last_name=\' + lastname + \'&message_phone=\' + phone; var data = { \\\"message_first_name\\\": firstname, \\\"message_last_name\\\": lastname, \\\"message_phone\\\": phone, } if (firstname.trim() == \\\"\\\" || lastname.trim() == \\\"\\\" || phone.trim() == \\\"\\\") { alert(\'missing information\'); } else { // AJAX Code To submit Form. $.ajax({ type: \\\"POST\\\", url: \\\"process.php\\\", data: data, cache: false, success: function(result) { console.log(result); alert(\'success\'); } }); } }); |
NOTE: You are missing email
and message
in the code. So the line if(firstname.trim() == \\\"\\\" || lastname.trim() == \\\"\\\" || email.trim() == \\\"\\\" || message.trim() == \\\"\\\")
may raise some errors and js skips the execution of remaining code
由于网站搬家,部分链接失效,如无法下载,请联系站长!谢谢支持!
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress ajax表单提交可能不使用wordpress功能吗?
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress ajax表单提交可能不使用wordpress功能吗?