0

I have a form and I want to insert the value of this form as soon as someone clicks a radio button. No better excuse than that I am not good at jQuery at all. This is what I've got so far.

HTML:

<form>
    <input type="radio" checked="true" id="q1r1" name="q1" value="Awesome">
    <label class="button1" for="q1r1">Awesome</label>

    <input type="radio" id="q1r2" name="q1" value="Ok">
    <label class="button2" for="q1r2">Ok</label>

    <input type="radio" id="q1r3" name="q1" value="Awful">
    <label class="button3" for="q1r3">Awful</label>
</form>

get.JSON:

$('#q1r1').on('click',function(){
    var get_var_name = $(this).val();
        $.getJSON('json/question1.php?team=<?php echo $teamid ?>',{q1:get_var_name},function(data,status){
            console.log(data);
            if(status == 'success'){
                console.log(data);
            } else {
                alert('Status: ' + status);
            }
            });
        });

...json/question1.php:

<?php
    require('db_connect.php');
    session_start();
    $uid = $_SESSION['uid'];

    if($_GET['q1']) {

        $teamid = $_GET['team'];
        $insertq1 = $_GET['q1'];

        $sql = "UPDATE result 
        SET q1='$insertq1'
        WHERE userid='$uid' AND teamid='$teamid' ";

        $result = $mysqli->query($sql);

    }
?>

As you probably see, I'm really no good at this at all, but this is what I had planned to happen:

  1. The logged in user selects an option, Awesome, OK or Awful.
  2. Without the page refreshing or a submit button being clicked, the get.JSON will automatically nudge the question1.php with the values teamid (saved in $teamid) and q1 (the value of the users choice).
  3. The php file silently updates the users database with the values, everybody happy.

Thing is that nothing happens when I click on "Awesome" as of right now, and I don't know why. I've been trying to follow multiple guides, but I'm just not sure how to go about solving this one. Any advice or help is much appreciated.

Kind regards, Mo.

P.s!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

is included.

Mo Mononoke
  • 175
  • 1
  • 11
  • Nothing happens right now because `{q1:get_var_name}` isn't valid javascript (or PHP). – Draco18s no longer trusts SE Dec 14 '15 at 18:10
  • @Draco18s Whats wrong with that syntax? – Steve Dec 14 '15 at 18:21
  • Oh, sorry, I missed where `get_var_name` as a var object. I was seeing it as some kind of "I don't know what to put here" TODO. – Draco18s no longer trusts SE Dec 14 '15 at 18:24
  • Ok, I'm at a loss. I don't see the problem. Have you tried logging the AJAX request url (to check that it's what you expect)? Have you tried hard-coding the data (as a test)? Are you sure that the second PHP document is valid (try testing in the browser)? – Draco18s no longer trusts SE Dec 14 '15 at 18:32
  • Stupid question but is your jQuery function in your `php` page or a separate `.js` file? – John C Dec 14 '15 at 18:38
  • are you sure the click event is firing? – Sergio S Dec 14 '15 at 18:50
  • wait, shouldn't you be looking for the 'change' event.. not the 'click' for radio buttons? – Sergio S Dec 14 '15 at 18:52
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 14 '15 at 19:32
  • You are calling a JavaScript function called `getJSON`. Where is the part of the PHP which actually sends the JSON back? – Quentin Dec 14 '15 at 19:33
  • I'm a complete newb here. I don't need anything back, I just need it to update the db. My jQuery function is on the same page as my html, but the php page is separate. I know I'm vulnerable to SQL injection attacks, and I do not need to defend myself from them. – Mo Mononoke Dec 14 '15 at 22:07

1 Answers1

0

just add onchange() in your input(radio type in this case) to call a javascript function that get data from url with no need to refresh the page, i've modified your code and tested it with a php of my own.

your html must be like

  <form>
      <input type="radio" checked="true" id="q1r1" name="q1" value="Awesome" onchange="GrabData(this)">
      <label class="button1" for="q1r1">Awesome</label>

      <input type="radio" id="q1r2" name="q1" value="Ok" onchange="GrabData(this)">
      <label class="button2" for="q1r2">Ok</label>

      <input type="radio" id="q1r3" name="q1" value="Awful" onchange="GrabData(this)">
      <label class="button3" for="q1r3">Awful</label>
  </form>

then you can either write this javascript code inside the html by adding

<script>.......</script>

or put it in another file .js (dont forget to call it from you html page)

JS CODE-->

function GrabData(Passeddata){
  var radioValue = Passeddata.value;
  var URL = "json/question1.php?q1="  + radioValue + "&teamid="<?php echo $teamid; ?>;
  $.ajax( {
      url : URL,
      type : "GET",
      dataType: 'json',
      success : function(data) {
        if (data == "") {
          alert("data is empty");
        }else{
          console.log('got your data back sir');
        }
      }
  });
}

please let me know if it worked or if there is some other problems

Med Abida
  • 1,214
  • 11
  • 31
  • How will that work? How do I insert Awesome/OK/Awful in dataYouWantToPass, and how do I add the teamid ($teamid) in that data? – Mo Mononoke Dec 14 '15 at 19:27
  • dataYouWantToPass can be anything you want to pass in your case it would be var URL = "json/question1.php?q1=" + radioValue + "&teamid="; – Med Abida Dec 14 '15 at 19:32
  • Hi Med. 1. I got an error saying GrabData was undefined in the log, and that there was an unexpected number in ""&teamid=";". I kinda figured the unexpected number was due to it being outside without any +. So I added a + between "&teamid=" + . Then nothing at all happened. The adress bar did not change, the php script was not run. Console log returns nothing. – Mo Mononoke Dec 14 '15 at 22:04
  • I still appreciate the help very much though Med Abida, thanks. – Mo Mononoke Dec 14 '15 at 22:04
  • 1-did you add the onchange() in your html? – Med Abida Dec 14 '15 at 22:07
  • 2- is the javascript on a diffrent file or on the same html? – Med Abida Dec 14 '15 at 22:08
  • 1. The JavaScript is in the html file, same as the form but not the php. – Mo Mononoke Dec 14 '15 at 22:18
  • 2. I added the onchange, and I tested it with "alert("Test");" in the first line of the function. It works. – Mo Mononoke Dec 14 '15 at 22:19
  • The other way around, sorry. – Mo Mononoke Dec 14 '15 at 22:19
  • The problem is more than likely a problem on my part somehow, don't feel obliged to waste your time! – Mo Mononoke Dec 14 '15 at 22:27
  • move your javascript code outside of the form ! put it just in the body below the form and dont foget to put it between Could you please paste a copy of your full html file here or edit your post – Med Abida Dec 14 '15 at 22:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97906/discussion-between-med-abida-and-mo-mononoke). – Med Abida Dec 14 '15 at 22:34