问题是,POST似乎没有向PHP脚本发送信息。 firebug控制台显示信息是从JavaScript传递的,并且看起来格式正确,但我没有得到响应,PHP脚本也没有运行。
然而,PHP本身工作正常。
Jquery:
$(function(){
$('#submit-item').live("click",function(){
var productName = $(".add-item-form #productName").val();
var category = $(".add-item-form #categorySelect").val();
var brandName = $(".add-item-form #brandName").val();
var volume = $(".add-item-form #volumeType").val();
$.post("addNewProduct.php",{productName:productName ,category: category, brandName: brandName, volume: volume});
});
});
HTML表单:
<section id="add-item-pane" class="secondary">
<header>
<h1>add item</h1>
<a href="#" class="close-secondary"></a>
</header>
<form class="add-item-form" method="POST" action="" accept-charset=utf-8>
<div class="input">
<label for="productName">product name</label>
<input type="text" name="productName" id="productName" placeholder="Product Name">
</div>
<div class="input">
<label for="categorySelect" required>category</label>
<select name="categorySelect" id="categorySelect" placeholder="Category" required>
<option value="1">ATERIA-AINEKSET JA KASTIKKEET</option>
</select>
</div>
<div class="input">
<label for="brandName">brand name</label>
<input type="text" name="brandName" id="brandName" placeholder="Brand Name">
</div>
<div class="input">
<label for="volumeType">volume type</label>
<label for="volumeTypeKG">g/kg</label>
<input type="radio" name="volumeType" id="volumeType" value="1">
<label for="volumeTypeL">ml/l</label>
<input type="radio" name="volumeType" id="volumeType" value="2">
</div>
<input type="submit" value="Add it" id="submit-item">
</form>
</section>
接收PHP:
$productName = $_POST['productName'];
$categoryId = $_POST['category'];
$brandId = $_POST['brandName'];
$volumeId = $_POST['volume'];
答案 0 :(得分:0)
您的代码中存在一些错误:
1)您有2个输入具有相同的 id ,根据定义,ID在页面中应该是唯一的。
<input type="radio" name="volumeType" id="volumeType" value="1">
<input type="radio" name="volumeType" id="volumeType" value="2">
如果要检索所选电台的值,则应为每个单选按钮添加一个CSS类,例如“myRadioGroup”,并使用以下内容检索该值:
$(".myRadioGroup:checked").val()
2)你有一个指向自身的表单(action=""
),你有一个input type="submit"
。因此,当您单击“添加”按钮时,侦听器将启动作业,但您不会停止提交表单并重新加载同一页面的提交事件。
尝试替换你的:
<input type="submit" value="Add it" id="submit-item">
使用一个简单的按钮,不会启动任何提交事件。
<button type="button" id="submit-item">Add it</button>
此外,表单在您的情况下是无用的,因为您不使用它来序列化您的数据。您也可以删除表单。
当您点击按钮时,它将不会提交任何内容,并且您将发送AJAX呼叫。