0

The following code works fine, the function load() sends the selected radio button info to the PHP page and display the returned:

<head>
<script>
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('myPHPfile.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
    <input type="radio" name="category" value="30" />ButtonC
  </div>
  <div id="myDiv">Click the button to load results</div>
</body>

myPHPfile.php

<?php 
  if( $_REQUEST["selectedButtonValue"] )
  {
     $buttonPHP = $_REQUEST['selectedButtonValue'];
     echo "Value button is ". $buttonPHP;
  }
?>

I need an alert box message of the returned PHP value, inside the script, as follows:

;

  $("#myDiv").load('myPHPfile.php', {selectedButtonValue : buttonValue});
  alert(<?php $buttonPHP ?>);

;

Would it be possible to write the PHP value inside JavaScript?

blsn
  • 1,077
  • 3
  • 18
  • 38

4 Answers4

1

Yes, almost had it:

alert('<?php echo $buttonPHP; ?>');
Nick Jonas
  • 1,197
  • 4
  • 13
  • 24
1

You can just add a callback function in .load(), like this:

$("#myDiv").load('myPHPfile.php', 
    {selectedButtonValue : buttonValue}, 
    function(data){
        alert(data);
});

If you only want the value of the $buttonPHP to be displayed in the alert, change your echo to

echo $buttonPHP;

instead of

echo "Value button is ". $buttonPHP;

*Note: There is an issue with .load() function. It will not work if you access your html page locally/directly, you need to put it on a server. Or you can use xampp, wampserver, etc. *

Hope it helps.

dunli
  • 1,356
  • 9
  • 18
  • Thank you for your reply, tested and working fine (an alert box message appears), but I need $buttonPHP as mentioned before, alert(''). The reason for that is because I need to use this value for another code in the same JavaScript. – blsn Jan 10 '13 at 01:04
  • In my sample, the alerted value is the value of `$buttonPHP`. If you want to use the value outside the callback function, just assign it to a `var` like: `var buttnPHP = data;` – dunli Jan 10 '13 at 01:07
  • But then again, it will not be a PHP value. I must get it as PHP value since inside my JavaScript there is a PHP loop which is using this value. Please could you help me on this matter. – blsn Jan 10 '13 at 01:15
  • Can you post the php loop inside your script? – dunli Jan 10 '13 at 01:19
  • _exportVariableToView(77); ?> – blsn Jan 10 '13 at 01:23
  • just need to replace the number 77 with $ buttonPHP – blsn Jan 10 '13 at 01:25
  • Uh.... I can't seem to visualize what you want to do. Can you update your post(question) and put the whole script which contains that loop? – dunli Jan 10 '13 at 01:28
  • Please see the link below: http://stackoverflow.com/questions/14185331/exchanging-variables-between-php-and-javascript – blsn Jan 10 '13 at 01:31
  • Try using `$_SESSION` in php so that you can use `$buttonPHP` in another page. [This](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) might help. – dunli Jan 10 '13 at 02:16
  • Thanks for trying to help me, I’ve tried the $_SESSION solution, but it didn’t do anything. – blsn Jan 10 '13 at 15:09
  • @user203952: Did you put a `session_start();` in your `myPHPfile.php` and in your `html`? – dunli Jan 11 '13 at 00:07
0

Your doing it right now, but you need to output the value:

alert(<?php echo $buttonPHP ?>);

On a sidenote, it seems like you don't have it really clear to you what happens. HTML, css and javascript runs on the clients(the visitors) computer and the PHP code runs on the server, that means that the you can always combine php output with client-side code, and this is done regularly, just as you have done.

This is also one of the reasons why PHP is so flamed as a language, because of it low threshold to get started with, it is way to easy to create code that works, but that is a hell to maintain.

This is not a comment to your code, but a general reflection on php.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
0

Either, you have to put the alert in the PHP generated code:

echo "<script type=\"text/javascript\">alert('Value button is ". $buttonPHP . "');</script>";

Or you have to use $.get function:

$.get('myPHPfile.php', {selectedButtonValue : buttonValue}, function(data) {
    alert(data);
});
tmuguet
  • 1,165
  • 6
  • 10
  • Thank you for the ‘$get()’ direction, the syntax is not working, hopefully I will try to indent your code. – blsn Jan 10 '13 at 00:13