我一直在整理我的第一个 Ajax 驱动的表单(此处),最终目标是将表单数据发送到 MySQL 数据库。我对 PHP 和 Javascript 的经验很少,因此采用 JQuery Formwizard 作为我的选择路径。该表单效果很好,但是我没有太多运气找到资源来帮助我编写我的“process.php”脚本。
默认情况下,表单当前返回一个字符串,例如 item1=foo&item2=bar...,它会在提交时显示。我附上了下面的 JS 和我的 process.php 文件的要点,但我不确定这两者是如何联系在一起的。提交按钮似乎根本没有指向脚本。表格有点长,但很简单,所以我只包含了一些。非常感谢,
麦克风
HTML (片段)
<form action="path/to/process-form.php" method="POST" id="demoForm" class="bbq">
<div id="fieldWrapper">
<span class="step" id="first">
<span class="font_normal_07em_black">Manufacturer Information</span><br />
<p class="section-desc">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In non est est. Quisque sit amet tellus nulla,
eget molestie velit. Morbi dapibus ornare purus eget ultrices. In et massa ac diam vulputate auctor.
Curabitur ut velit nisi, vel suscipit ligula. Donec non tellus id mauris ornare laoreet sed quis massa.
</p>
<br />
<div class="clear-line"></div>
<br />
<label for="manu_name">Manufacturer</label><br />
<input class="input_field_12em" name="manu_name" id="manu_name" /><br />
<label for="manu_web">Website</label><br />
<input class="input_field_12em" name="manu_web" id="manu_web" value="http://" /><br />
<label for="manu_email">Email</label><br />
<input class="input_field_12em" name="manu_email" id="manu_email" /><br />
<label for="manu_phone">Phone</label><br />
<input class="input_field_12em" name="manu_phone" id="manu_phone" />
<label for="manu_phone_ext">ext</label>
<input class="input_field_12em" name="manu_phone_ext" id="manu_phone_ext" /><br />
<label for="manu_info">Additional Info</label><br />
<textarea class="input_field_12em" name="manu_info" id="manu_info"></textarea><br />
</span>
...继续
<div id="demoNavigation">
<input class="navigation_button" id="back" value="Back" type="reset" />
<input class="navigation_button" id="next" value="Next" type="submit" />
</div>
</form>
JS
$(function(){
$("#demoForm").formwizard({
formPluginEnabled: true,
validationEnabled: true,
focusFirstInput : true,
formOptions :{
type:'POST',
success: function(data){$("#status").fadeTo(5000,1,function(){ $(this).html("Product successfully submitted!").fadeTo(5000, 0); })},
beforeSubmit: function(data){$("#data").html("data sent to the server: " + $.param(data));},
dataType: 'json',
resetForm: true
},
validationOptions : {
rules: {
<!-- Manufacturer validation -->
manu_name: "required",
manu_email: {
email: true
},
},
messages: {
required: "This field is required.",
email: "Please enter a valid email address."
}
}
});
});
PHP (过程-form.php)
<?php
include('dbconnect.php');
/* manufacturer */
$manu_name = $_POST['manu_name'];
$manu_web = $_POST['manu_web'];
$manu_email = $_POST['manu_email'];
$manu_phone = $_POST['manu_phone'];
$manu_phone_ext = $_POST['manu_phone_ext'];
$manu_info = $_POST['manu_info'];
$manu_name = mysqli_real_escape_string($manu_name);
$manu_web = mysqli_real_escape_string($manu_web);
$manu_email = mysqli_real_escape_string($manu_email);
$manu_phone = mysqli_real_escape_string($manu_phone);
$manu_phone_ext = mysqli_real_escape_string($manu_phone_ext);
$manu_info = mysqli_real_escape_string($manu_info);
$query = "INSERT INTO pd_products
(manu_name,manu_web,manu_email,manu_phone,manu_phone_ext,manu_info)
VALUES ('$manu_name','$manu_web','$manu_email','$manu_phone','$manu_phone_ext','$manu_info')";
if (!mysqli_query($query,$con))
{
die('Error: ' . mysqli_error());
}
mysqli_close($con)
?>