2017-08-20 3 views
0

データベースに変数値を送信するためにJquery変数をPHPに渡そうとしています。私のPHPコード、HTMLコード、および私のjQueryコードは、entrysurvey.phpという同じコードファイルにあります。私はラジオボタンがクリックされたときにそのイメージがjquery変数を定義するために使われているという属性を持っています。これは成功しました。コードのアラート部分には、イメージに対応する正しい変数番号が表示されます。しかし、私はまだPHPにそれを渡すためにAJAXを利用することに成功していませんでした。私は多くの異なるonSubmit関数を試しましたが、これまでに何も機能していませんでした。どんな助けもありがとう。データベース提出のためにPHPにJquery変数を渡す

私のjQueryのコード:

<script type="text/javascript" src="javascript/jquery-3.2.1.min.js"> </script> 
 

 
\t <script type="text/javascript"> 
 

 
\t $(document).ready(function(){ 
 

 
\t \t \t var value 
 
\t \t \t $('img').click(function(){ 
 
\t \t  $('.selected').removeClass('selected'); 
 
\t \t  $(this).addClass('selected'); 
 
\t \t // var value = $(this).val(); 
 
\t \t // alert(value); 
 
\t \t \t if($(this).attr("alt") == "first image"){ 
 

 
\t \t \t \t value = "1"; 
 
\t \t \t \t alert(value); 
 

 

 

 

 
\t \t \t \t 
 
\t \t \t } else if($(this).attr("alt") == "second image"){ 
 

 
\t \t \t \t value = "2"; 
 
\t \t \t \t alert(value); 
 

 
\t \t \t }else if($(this).attr("alt") == "third image"){ 
 

 
\t \t \t \t value="3" 
 
\t \t \t \t alert(value); 
 
\t \t \t }else if($(this).attr("alt")== "fourth image"){ 
 

 
\t \t \t \t value="4" 
 
\t \t \t \t alert(value); 
 
\t \t \t }else if($(this).attr("alt")=="fifth image"){ 
 

 
\t \t \t \t value="5"; 
 
\t \t \t \t alert(value); 
 
    \t \t \t \t }else if($(this).attr("alt")=="sixth image"){ 
 

 
\t \t \t \t value="6"; 
 
\t \t \t \t alert(value); 
 
\t \t \t }else if($(this).attr("alt")=="seventh image"){ 
 

 
\t \t \t \t value="7"; 
 
\t \t \t \t alert(value); 
 
\t \t \t } 
 

 

 

 
\t \t \t \t \t $('.submitButton').click(function() { 
 
if (value != null) { //value is a variable so you can write it like this 
 
    $.post('entrysurvey.php', {val: value}, function(response) { 
 
     alert(response); 
 
    }); 
 
} 
 
}); 
 

 

 

 

 

 
\t 
 

 
\t \t \t }); 
 

 

 

 

 

 

 

 

 
\t \t \t 
 

 
\t \t \t 
 

 
\t }); 
 

 

 

 

 

 

 
\t </script>

HTMLコード:

 <form action="entrysurvey.php" method="POST" id="target"> 
 
     
 
    Select the image that you feel resembles your relationship with your pair the most: <br> <br> 
 

 
     <table cellpadding="20"> 
 
    \t \t \t <tbody> 
 
    \t \t \t \t \t <tr> 
 
    \t \t \t \t \t \t \t <td><input type="radio"  name="oneness" value = "1" class="oneness" > <br> <br> <br> <img src="images/1.png" height="200px" width="200px" class="selectedImage" alt="first image" > <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td> <input type="radio" name="oneness" value = "2" class="oneness" > <br> <br> <img src="images/2.png" height = "200px" width="200px" class="selectedImage" alt="second image" > <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td><input type="radio" name="oneness" value = "3" class="oneness" > <br> <br> <img src="images/3.png" height = "200px" width="200px" class="selectedImage" alt="third image" > <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td>&nbsp;</td> 
 
    \t \t \t \t \t \t \t </tr> 
 
    \t \t \t \t \t \t </tbody> 
 
    \t \t \t \t \t </table> 
 

 
    \t \t \t \t \t <table cellpadding="20"> 
 
    \t \t \t \t \t \t <tbody> 
 
    \t \t \t \t \t \t \t <tr> 
 
    \t \t \t \t \t \t \t \t <td><input type="radio" name="oneness" value = "4" class="oneness" > <br> <br> <img src="images/4.png" height = "200px" width="200px" class="selectedImage" alt="fourth image" > <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td><input type="radio" name="oneness" value = "5" class="oneness" > <br> <br> <img src="images/5.png" height = "200px" width="200px" class="selectedImage" alt="fifth image" > <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td><input type="radio" name="oneness" value = "6" class="oneness" > <br> <br> <img src="images/6.png" height = "200px" width="200px" class="selectedImage" alt="sixth image"> <br> <br></td> 
 
    \t \t \t \t \t \t \t \t <td><input type="radio" name="oneness" value = "7" class="oneness" > <br> <br> <img src="images/7.png" height = "200px" width="200px" class="selectedImage" alt="seventh image"> <br> <br></td> 
 
    \t \t \t \t \t \t \t </tr> 
 
    \t \t \t \t \t \t </tbody> 
 
    \t \t \t \t \t </table> 
 
       
 
    <input type="submit" name="submit" value="Submit" class="submitButton"> 
 
    \t \t \t </form>

最後に、これは私がpしようとするために使用しています機能ですそれをオフにPHPをしてください。

$('.submitButton').click(function(){ if($(value) != null){ $.post('entrysurvey.php', 'val=' + $(value).val(), function(response){ alert(response); }); } });

これは私のPHPコードです:

- スニペットを開始!:JS非表示:偽コンソール:真バベル:偽 - >

<!-- language: lang-php--> 

まだ残念ながら何も起こりません。 hはvalの値を取ることになって、まだnullの場合、これはエラー/レスポンスである私が取得:

enter image description here

+2

あなたのajax機能はどこですか? –

+0

$( '。submitButton')。click(function(){ \t \t \t if($(value)!= NULL){ \t \t \t \t $ .post( 'entrysurvey.php'、 'ヴァル=' + $(値).val()、関数(応答){ \t \t \t \t \tアラート(応答)。 \t \t \t \t}); \t \t \t} \t \t})。これだよ! – AAA

+0

他人が正しく読むことができるように、ajax関数で質問を更新してください。私はそれを編集した:) –

答えて

0

あなたはAjaxコードを掲示しなければなりません!問題は、あなたの値変数がif文の中で宣言されていることです。これは、変数のスコープがifステートメント内にあることを意味します。すべての文の外にそれを宣言するようにしてください、私はあなたがこれは

+0

$( 'のsubmitButtonを')。($(値)場合は(関数(){ \t \t \tをクリックして提案を – AAA

0

は、まあ、私が編集した役割を果たし得ることを場合には、あなたがいない整数として、文字列として、あなたの値変数を掲示していることを認識する必要があります

$(document).ready(function(){ 
    var value; 
    //the other part of code down here 

をしましょういくつかのエラーがあるので、あなたのAjaxコード!

$('.submitButton').click(function() { 
    if (value != null) { //value is a variable so you can write it like this 
     $.post('entrysurvey.php', {val: value}, function(response) { 
      alert(response); 
     }); 
    } 
}); 

今サーバーに変数を送信するために右の構文は、この{:値valの}のようなJSON形式で送信しています!したがって、変数valをサーバーに送信しています。この変数の値はvalue変数の値です。あなたがpostメソッドを使っているので、あなたのentrysurvey.phpファイルでこれを使うことができます。 $ variable_name = $ _POST ['val']; この方法で、PHPファイルはあなたが送りたい値を得ます!!!

+0

あなたのコードと一致するよう上記のコードを編集しましたが、残念ながらvalの値はまだnullです。 – AAA

+0

入力タイプをよく変更してください。ただのボタンにしよう。それがクリックされたとき、それはくすんだajaxの部分が –

+0

に来るところであり、あなたはajaxを使うときにformタグを取り除くことができます、サーバにデータを送信するためにformタグを使うことは必須ではありません –

関連する問題