我正在构建一个表单,要求用户提供名称,地址, amount1 , amount2 以及评论。
无线电是amount1和amount2的不同值。
当我提交表单时,我的变量$rate_error
会返回“您必须输入评分评论”; 即使它不是空白。
我的无线电输入可能是问题吗?
我是否错误地指定了amount1
变量?
当用户选择不同的金额时,我需要更改变量的值。
<form id='main-rate' method="POST">
<input type="search" name="address" id="geocomplete" placeholder="Type in an address"/>
<input type="text" name='landlord_name id="name" '/>
<input type="radio" name="amount1" id="amount1" value="2">
<label for"amount1">2<label>
<input type="radio" name="amount1" id="amount1" value="4">
<label for"amount1">4<label>
<input type="radio" name="amount1" id="amount1" value="6">
<label for"amount1">6<label>
<input type="radio" name="amount1" id="amount1" value="8">
<label for"amount1">8<label>
<input type="radio" name="amount1" id="amount1" value="10">
<label for"amount1">10<label>
<input type="radio" name="amount2" id="amount2" value="2">
<label for"amount2">2<label>
<input type="radio" name="amount2" id="amount2" value="4">
<label for"amount2">4<label>
<input type="radio" name="amount2" id="amount2" value="6">
<label for"amount2">6<label>
<input type="radio" name="amount2" id="amount2" value="8">
<label for"amount2">8<label>
<input type="radio" name="amount2" id="amount2" value="10">
<label for"amount2">10<label>
<textarea id="comment" name="comment" required="required">
</textarea>
<button type='submit'>Submit</button>
</form>
$(document).ready(function()
{
$("#main-rate").submit(function()
{
var address = $('#geocomplete').val();
var name = $("#name").val();
var comment = $('#comment').val();
var amount1 = document.getElementsByName('amount1');
for (var i = amount1.length; i--;) {
amount1[i].onchange = function() {
amount1 = this.value;
console.log(amount1);
}
}
var amount2 = document.getElementsByName('amount2');
for (var i = amount2.length; i--;) {
amount2[i].onchange = function() {
amount1 = this.value;
console.log(amount2);
}
}
var dataString = name + address + amount1 + amount2 + comment;
if (name == '' || address == '' || amount1 == '' || amount2 == '' || comment == '')
{
console.log("Please Fill All Fields");
} else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "path/to/url",
data: dataString,
cache: false,
success: function(result)
{
console.log(result);
$('rate-section').html(
'<div><h1>THANKS FOR RATING!</h1></div>'
);
return false;
}
});
}
return false;
});
});
$rate_error = 0;
if (isset($comment) && $comment != '') {
if (!$address_id && !$address) {
$rate_error = "must enter an address";
}
//if not logged in and creating new user
if (!$SESSION->logged_in()) {
$User = new USER();
$rate_error = $User->createUser($username_entered, $email_entered, $pass_1, $pass_2, $fb_user, $g_user, $g_token, 1);
}
if (!$rate_error && !$SESSION->user_id()) {
$rate_error = "error - you must create an account before rating a landlord";
}
if (!$comment) {
$rate_error = "you cannot leave a blank review";
}
if (!$landlord_id && !$landlord_name) {
$rate_error = "you must enter a landlord name";
}
} else {
$rate_error = "you must enter a rating comment";
}
if (($landlord_name || $landlord_id) && $rate_error==0) {
//query to database
$success = 1;
}
if ($success){
$resultArray = array("success"=>1);
$resultArray['landlord'] = $current_landlord;
} else {
$resultArray = array("success"=>0,
"rate_error"=>$rate_error);
}
对我的javascript进行了一些更改,但现在收到500内部错误。
$("#main-rate").submit(function()
{
var address = $('#geocomplete').val();
var name = $("input[name='landlord_name']").val();
var amount1 = $("input[name='amount1']").val();
var amount2 = $("input[name='amount2']").val();
var amount3 = $("input[name='amount3']").val();
var amount4 = $("input[name='amount4']").val();
var amount5 = $("input[name='amount5']").val();
var amount6 = $("input[name='amount6']").val();
var amount7 = $("input[name='amount7']").val();
var comment = $('#comment').val();
var dataString = {
name: name,
address: address,
amount1: amount1,
amount2: amount2,
amount3: amount3,
amount4: amount4,
amount5: amount5,
amount6: amount6,
amount7: amount7,
comment: comment
}
if (dataString.name == '' || dataString.address == '' || dataString.amount1 == '' || dataString.amount2 == '' || dataString.amount3 == '' || dataString.amount4 == '' || dataString.amount5 == '' || dataString.amount6 == '' || dataString.amount7 == '' || dataString.comment == '')
{
console.log("Please Fill All Fields");
} else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "path/to/url",
data: dataString,
cache: false,
success: function(result)
{
console.log(result);
$('rate-section').html(
'<div><h1>THANKS FOR RATING!</h1></div>'
);
return false;
}
});
}
return false;
});
});
答案 0 :(得分:0)
正如@ShaunakD指出的那样,您没有正确地将数据发送到服务器。如果您的所有输入都填写了字母'a',它仍会提交,但PHP正在接收'aaaaa'(嗯,从技术角度来看,考虑到下面的无线电和金额1和2分配问题),没有任何迹象表明变量是什么过去了。
我建议他的第一种方法将你的dataString赋值行改为:
var dataString = {
name: name,
address: address,
amount1: amount1,
amount2: amount2,
comment: comment
};
您当前的实施将在检索amount1和amount2的值时遇到问题。您在提交函数中为onchange分配事件。您应首先删除每个无线电输入的id属性,并用以下内容替换它们的值赋值(更简单):
var amount1 = $("input[name='amount1']").val();
var amount2 = $("input[name='amount2']").val();
编辑:为我的第一条评论提供更多背景信息:
$("#main-rate").submit(function()
{
var address = $('#geocomplete').val();
var name = $("#name").val();
var comment = $('#comment').val();
var amount1 = $('input[name="amount1"]').val();
var amount2 = $('input[name="amount2"]').val();
//Remove this entirely
/*
var amount1 = document.getElementsByName('amount1');
for (var i = amount1.length; i--;) {
amount1[i].onchange = function() {
amount1 = this.value;
console.log(amount1);
}
}
var amount2 = document.getElementsByName('amount2');
for (var i = amount2.length; i--;) {
amount2[i].onchange = function() {
amount1 = this.value;
console.log(amount2);
}
}
*/
祝你好运!