Passing Jquery Variable to PHP for database submission -
i attempting pass jquery variable php in order submit variable value database. php code, html code, , jquery code in same code file called entrysurvey.php. have radio button image when being clicked attribute being used define jquery variable. successful, alert portion of code displays correct variable number corresponding image. however, not successful in utilizing ajax in order pass off php yet. have tried many different onsubmit functions nothing has worked far. appreciated.
my jquery code:
<script type="text/javascript" src="javascript/jquery-3.2.1.min.js"> </script> <script type="text/javascript"> $(document).ready(function(){ var value $('img').click(function(){ $('.selected').removeclass('selected'); $(this).addclass('selected'); // var value = $(this).val(); // alert(value); if($(this).attr("alt") == "first image"){ value = "1"; alert(value); } else if($(this).attr("alt") == "second image"){ value = "2"; alert(value); }else if($(this).attr("alt") == "third image"){ value="3" alert(value); }else if($(this).attr("alt")== "fourth image"){ value="4" alert(value); }else if($(this).attr("alt")=="fifth image"){ value="5"; alert(value); }else if($(this).attr("alt")=="sixth image"){ value="6"; alert(value); }else if($(this).attr("alt")=="seventh image"){ value="7"; alert(value); } $('.submitbutton').click(function() { if (value != null) { //value variable can write $.post('entrysurvey.php', {val: value}, function(response) { alert(response); }); } }); }); }); </script>
html code:
<form action="entrysurvey.php" method="post" id="target"> select image feel resembles relationship pair most: <br> <br> <table cellpadding="20"> <tbody> <tr> <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> <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> <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> <td> </td> </tr> </tbody> </table> <table cellpadding="20"> <tbody> <tr> <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> <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> <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> <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> </tr> </tbody> </table> <input type="submit" name="submit" value="submit" class="submitbutton"> </form>
lastly function using attempt pass off php.
$('.submitbutton').click(function(){ if($(value) != null){ $.post('entrysurvey.php', 'val=' + $(value).val(), function(response){ alert(response); }); } });
this php code:
!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-php-->
and still nothing happens unfortunately ios supposed take value of val still null, , error / response get:
you must post ajax code! problem, guess, value variable declared inside if statement. means variable's scope within if statement. try declare outside statement, let me say
$(document).ready(function(){ var value; //the other part of code down here
you must aware posting value variable string, not integer, in case may serve
Comments
Post a Comment