データベースに変数値を送信するために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> </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の場合、これはエラー/レスポンスである私が取得:
あなたのajax機能はどこですか? –
$( '。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
他人が正しく読むことができるように、ajax関数で質問を更新してください。私はそれを編集した:) –