1

How to pass the variable from parameter of javascript into PHP... I try whatever I think... but it's too hard... this is the code.. I pass the text and used it on my $pieces I don't know what is the right code.

for example I have the onclick like this

<input type="radio" name="selectplaneDepart" id="selectplaneDepart" value="PUNX-69;Cebu;Philippines;2014-02-10 06:00:00;Metro Manila;Philippines;2014-02-10 10:00:00;1200" onclick="writeResultReturn(value); " />

function writeResultReturn(text) {
    <?php 
        $pieces = explode(";", text );
    ?>
}
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • 5
    Its not possible. As javascript run at client side. You can do with AJAX – Roopendra Feb 10 '14 at 05:50
  • 1
    You are not doing it right. PHP script runs in server side before the output comes to browser. JavaScript executes in browser. You need add something to Cookie through Javascript and in Server side in PH read it from cookie. – Nish Feb 10 '14 at 05:51
  • Instead of `explode of php` you can use `split of JavaScript` and this can be achieve by `AJAX`. – Kaushik Feb 10 '14 at 05:59
  • 1
    use ajax to pass that variable to your server side php code. – Altaf Hussain Feb 10 '14 at 06:19

1 Answers1

0

As I have mentioned in my comment its not possible in javascript. But you can achieve by using AJAX as below.

Html:-

<input type="radio" name="selectplaneDepart" id="selectplaneDepart" value="PUNX-69;Cebu;Philippines;2014-02-10 06:00:00;Metro Manila;Philippines;2014-02-10 10:00:00;1200" />

jQuery:-

$('#selectplaneDepart').click(function() {
    var selectplaneDepart = $(this).val();
    $.ajax({
      url: 'ajax.php',
      type: 'post',
      data: {'selectplaneDepart': selectplaneDepart},
      cache: false,
      success: function(response) {
        $('#responsDiv').html(response);
      },
      error: function(xhr, desc, err) {
        console.log(xhr + "\n" + err);
      }
    }); 
});

In ajax.php

<?php
if(isset($_POST['selectplaneDepart'])) {
    $pieces = explode(";", $_POST['selectplaneDepart']);
    // Here you can perform your expected operation using $pieces array :)
    echo 'success';
}
exit;
?>
Roopendra
  • 7,674
  • 16
  • 65
  • 92