2017-11-15 5 views
0

私はこのAJAXを使用してSQL挿入のためにPHPにデータを渡すために戦っていますが、データがPHPに渡されないようです。私は3つのHTML入力タイプ、テキスト、ラジオボタンと選択を持っています。ラジオはgenderで使用され、countryの住居が選択され、国のdialcodeのテキストがユーザーのmobilenumberと連結されています。 Javascriptは、ユーザーがドロップダウンリストでcountryを選択すると自動的にdialcodeを取得するために使用されます。これらの入力はSQL挿入のために渡されません。AJAXの複数の入力がPHPにデータを送信または送信しない

私はまだ非常にアマチュアと学生です。私はここのコードが含まれて次のように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)); 

     } 
}?> 
+1

ここで、 'data:'の中括弧は中かっこですか? – Ghost

+0

データを適切な形式で渡す必要があります。 (文脈から)Hey Masood私はエミレーツのWeb開発者であり、あなたと連絡を取りたいと思っています。 – Mohammed

+0

ねえ、私は喜んでいます。 [email protected]まで私に連絡することができます。 – Masood

答えて

1

あなたが適切にデータキーをワープする必要が

data: { 
      gender: gender, 
      country: country, 
      dialcode: dialcode, 
      mobile: mobile}, 

データキーに正しいフォーマットを渡していませんでした。あるいは、このエラーと間違いを避けるために、フォームを作成してください。

data:$('#userDetails').serialize(), 
関連する問題