AJAX没有提交POST数据

时间:2017-07-25 00:11:31

标签: php jquery ajax forms

通过jquery提交表单时,我似乎没有发送任何数据

我的问题的片段(代码简化为简化):

头:

        <script>

function dSubmit(formName, formAction, fieldsToCheck, divToHide){


        // No Errors process form


var url = formAction; // the script where you handle the form input.

$.ajax({
       type: "POST",
       url: url,
       data: $("#" + formName).serialize(), // serializes the form's elements.
       success: function(data)
       {alert(data)}
     });

return false; // avoid to execute the actual submit of the form.


}
</script>

体:

<a onclick="dSubmit('treatmentDetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>


<form name="treatmentdetails" id="treatmentdetails" 

action="processTreatments.php" method="POST">

    <input type="text" name="treatmentName" value="<?php echo $treatment;?>"/>
<input type="text" name="treatmentId" value="<?php echo $treatmentId;?>"/><br>

    </form>

php(treatments / processTreatments.php):

    $table  =   $dbPrefix."treatmentsOptions";

$treatmentId = $_POST['treatmentId'];
$treatmentName  = $_POST['treatmentName'];
$query = mysql_query("UPDATE $table SET name='$treatmentName' WHERE id='$treatmentId'");

echo "variables = id: $treatmentId name: $treatmentName"; // according to jquery submit neither variable appear

1 个答案:

答案 0 :(得分:1)

您输错了treatmentDetails,修正为treatmentdetails

$("#treatmentDetails").serialize()返回空字符串。

您的正确代码是:

<a onclick="dSubmit('treatmentdetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>