1

I have two variables, col1 and col2, in JavaScript, I need to send them to a PHP page when submit button is clicked.

I have the following code:

<script>
$("input:radio").change(function(){
  if($(this).is(":checked")){
    var col1 = $(this).parent().parent().children("td:first");
    var col2 = col1.next();
  }
})
</script>
4
  • If you them to be accessed by JavaScript, you could just save them in localStorage or you could pass them through the URL. Commented Jan 13, 2016 at 12:46
  • 1
    you can append the values in link and ajax call Commented Jan 13, 2016 at 12:46
  • You can assign them to some hidden inputs and submit the form. Commented Jan 13, 2016 at 12:48
  • @Script47 I couldn't get your question! Commented Jan 13, 2016 at 12:49

3 Answers 3

1

Use hidden elements which resides in form and assign values into it.

HTML

<form>
    <!--other code-->
    <input type="hidden" id="col1" name="col1">
    <input type="hidden" id="col2" name="col2">
</form>

JQUERY

$("input:radio").change(function(){
    var col1 = '', col2 = '';
    if($(this).is(":checked")){
        col1= $(this).parent().parent().children("td:first");
        col2 = col1.next();            
    }
    $('#col1').val(col1);
    $('#col2').val(col2);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Put it outside the if condition like the edited answer
0

You can use XMLHTTPRequest to process your data to PHP

 <script>
$("input:radio").change(function(){
  if($(this).is(":checked")){
    var col1 = $(this).parent().parent().children("td:first");
    var col2 = col1.next();

   $.ajax({
       url: "phpfile.php",
       data: {"col1":col1,"col2":col2},
       success:function(data){
          console.log(data);
       }
   });
  }
});
</script>

Comments

0

I need to send them to a PHP page when submit button is clicked.

So I can assume that there is a page refresh(unless you are using event.preventDefault()) , so hidden fields value will also get erased.

So you can use the local storage and save the values and on new page can again retrieve them and use it

$("input:radio").change(function(){
    var col1 = '', col2 = '';
    if($(this).is(":checked")){
        col1= $(this).parent().parent().children("td:first");
        col2 = col1.next(); 

     // Storing in local storage
      localStorage.setItem("col1", col1);
      localStorage.setItem("col2", col2); 

     // retrieve on next page 
     localStorage.getItem("col1");    // col1
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.