我一直在与这个AJAX争夺将数据传递给PHP for SQL Insertion,似乎数据没有传递给PHP。我有三种HTML输入类型,文本,单选按钮和选择。收音机用于gender
,选择住所的country
,以及与用户dialcode
连接的国家mobilenumber
的文字。一旦用户在下拉列表中选择了他/她dialcode
,Javascript就会自动检索country
。 SQL插入不传递这些输入。
我仍然是一名业余爱好者和学生。我在这里添加了代码:https://jsfiddle.net/406tb35r/1/
代码如下:
<script type="text/javascript">
(function($){
$(document).ready( function(){
$('#country').change( function(e){
//on change event
$('input[name="dialcode"]').val($(this).find('option:selected').data('co'));
});
}); //make sure all html is loaded before running
})(jQuery); //assign jQuery to the $
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
<form action="" method="post" class="start_form" id="userDetails">
<fieldset>
<legend><b>Are you a</b></legend>
<input type="radio" name="gender" class="gender" value="male">Male<br>
<input type="radio" name="gender" class="gender" value="female">Female<br>
</fieldset>
<select id="country" name="country" class="country">
<option data-co="" value="">Which country are from?</option>
<option data-co="+27" value="ZA">South Africa</option>
<option data-co="+376" value="AD">Andorra</option>
<option data-co="+971" value="AE">United Arab Emirates</option>
<option data-co="+93" value="AF">Afghanistan</option>
</select>
<?php
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
);
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes = ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option data-co='".$country["code"]."' value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>";
return $output;
}
?>
<input align="center" type="text" name="dialcode" class="dialcode" value="" autocomplete="off" placeholder="" mssg="" maxlength="6" readonly>
<input type="text" name="mobile" class="mobile" value="" autocomplete="off" placeholder="What is your Mobile Number?" mssg="" maxlength="12">
<input style="width:100%;" type="submit" name="s_submit" value="Next" id="userSubmit"><br/>
<a href="">Skip</a>
</form>
<script type="text/javascript">
jQuery(document).ready(function(){
$('#userSubmit').click(function(e){
e.preventDefault();
var gender = $('.gender').val();
var country = $('.country');
var dialcode = $('.dialcode');
var mobile = $('.mobile').val();
$.ajax({
type: 'POST',
url: DIR+'/insert.php',
data:
gender: gender,
country: country,
dialcode: dialcode,
mobile: mobile
success:function(){
$('.content').load(DIR+'/welcome.php');}
}
});
});
});
</script>
这是用于将上述数据传递给SQL的PHP:
<?php
session_start();
//Connect to DB
include_once 'connect.php';
$session = $_SESSION['id'];
$universal = new universal;
$susername = $universal->GETsDetails($session, "username");
if (isset($_POST['username'])) {
$gender = preg_replace("#[<> ]#i", "", $_POST['gender']);
$country = preg_replace("#[<> ]#i", "", $_POST['country']);
$mobilenumber = preg_replace("#[^0-9]#i", "", $_POST['dialcode'] . $_POST['mobile']);
if((!$username) == ""){
return "Your details are not logged in or do not have an account with iamstein!";
} else {
$users = $this->db->prepare("UPDATE users SET gender = :gender, country = :country, mobilenumber = :mobilenumber, WHERE id = :id");
$users->execute(array(":gender" => $gender, ":country" => $country, "mobilenumber" => $mobilenumber, ":id" => $session));
}
}?>
答案 0 :(得分:1)
您需要正确扭曲数据密钥
data: {
gender: gender,
country: country,
dialcode: dialcode,
mobile: mobile},
您没有为数据键传递正确的格式。或者您可以序列化表单,只是为了避免此错误和错误。
data:$('#userDetails').serialize(),