Getting radio button value and sending through ajax to php

前端 未结 3 749
不知归路
不知归路 2021-01-01 02:50

I have a poll on my website which displays radio buttons next to each answer. When the user selects an option and submits, im running a a php script via ajax to insert the v

相关标签:
3条回答
  • 2021-01-01 03:15
      $optionID = "="+optionID;
    

    I don't quite understand what you are trying to do here o.O, in javascript you don't define your variables using $.

    data: { optionID : option}
    

    using it like this should work. You would retrieve it like this in PHP:

    $option_value=$_POST['optionID'];
    
    0 讨论(0)
  • 2021-01-01 03:21
    var option = $('input[type="radio"]:checked').val();
    
    $.ajax({
        type: "POST",
        url: "ajax_submit_vote.php",
        data: { poll_option : option }
    });
    

    In the PHP you are reading $_POST['poll_option'] therefore you must use poll_option as the key in your data object. Also, the value is stored in option not $optionID as you were trying to use.

    The $ is a valid character in variable names in Javascript, it doesn't do anything special itself but some coders prefix anything that is a jQuery object with $ so they can glance through code and easily see what variables already have the jQuery wrapper.

    For example:

    var $option = $('input[type="radio"]:checked'); // $option is the jQuery wrapped HTML element
    var myValue = $option.val(); // we know $option already has the jQuery wrapper so no need to use the $(..) syntax.
    
    0 讨论(0)
  • 2021-01-01 03:26
    $("#submit_vote").click(function(e){ 
    
        $.ajax( {
          type: "POST",
          url: "ajax_submit_vote.php",
          data: $('#poll_form').serialize(),
          success: function( response ) {}
        });
    
    });
    

    You should then have the POST variable "poll_option" accessible in your PHP script.

    0 讨论(0)
提交回复
热议问题