-2

I'm trying to call a PHP function via radio button onclick event, but it isn't working. I'm trying to use Ajax method to call the function, code as follows:

test0.php (php file with radio buttons):

<?php

include "test1.php";

echo '<input type="radio" id="1" name="rere" value="qwqw" checked onclick="testFunc();"><label for="1">radio 1</label>';
echo '<input type="radio" id="1" name="rere" value="qwqw" onclick="testFunc();"><label for="1">radio 2</label>';

echo '<div><p id="res">sdfdsfsdfsd</p></div>';

echo condCheckedUpd();

?>

<script>
function testFunc() {
   $.ajax({
      url: 'test1.php',
      success: function(data) {
         //alert(data);
      }   
   });
}
</script>

test1.php (contains function to call)

<?php

function condCheckedUpd() {
   echo "works";
}

?>
Ivan
  • 34,531
  • 8
  • 55
  • 100
Pman
  • 13
  • 2

2 Answers2

0

You can't call a function in your php file directly from the client. You can, however, pass data back which lets you determine which function to call.

For example, you could send back a query string test1.php?functionName=condCheckedUpd, and then in your test1.php file, you can check for it:

<?php

function condCheckedUpd() {
    echo "works";
}

if ($_GET['functionName'] == 'condCheckedUpd') {
    condCheckedUpd();
}

?>
Shahzad
  • 2,033
  • 1
  • 16
  • 23
0

Think of your ajax call as if you're loading up a new tab in your browser. So, when you click the radio button, your call from test0.php is retrieving whatever gets responded to by test1.php, completely in isolation.

So, no need to include test1.php in your existing file- you're calling it separately! Your solution might be as simple as editing test1.php to execute the function when called, like so:

test0.php

<?php

//include "test1.php"; // no need to include this file here

echo '<input type="radio" id="1" name="rere" value="qwqw" checked onclick="testFunc();"><label for="1">radio 1</label>';
echo '<input type="radio" id="1" name="rere" value="qwqw" onclick="testFunc();"><label for="1">radio 2</label>';

echo '<div><p id="res">sdfdsfsdfsd</p></div>';

//echo condCheckedUpd(); //also no need for this function call here

?>

<script>
function testFunc() {
    $.ajax(
{
 url:     'test1.php',
 success: function(data) 
 {
  //alert(data);
 }   
});
}
</script>

test1.php

<?php
function condCheckedUpd() {
    echo "works";
}
condCheckedUpd();
?>

You also asked about passing parameters along with your request, for that there's the data setting that you can include. For example, replace your javascript in test0.php above with something like:

<script>
 //...^ all your existing php/html is still above
function testFunc() {
    $.ajax({
          url: "test0.php",
          data: { name: "John", location: "Boston" }
        })
          .done(function( msg ) {
            alert( "Data Saved: " + msg );
          });
}
</script>

Then, in test1.php, you can get your above parameters using the $_REQUEST global variable, and pass them into your function:

<?php
$getName = $_REQUEST['name'];
$getLocation = $_REQUEST['location'];

function condCheckedUpd($getName, $getLocation) {
    echo "works for ".$getName." in ".$getLocation;
}
condCheckedUpd();
?>

For your purposes, I expect you probably want to get the value of your radio buttons. For that, you might look into html/javascript's dataset attribute as an easy way to pass these along (Examples and docs here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/dataset).

Warning! If you're accepting values this way, be careful that what comes through in your $_REQUEST variables is what you expect, and be very careful if you end up displaying these back to the screen– lots of security concerns here. A few clues: How can I sanitize user input with PHP?

John P.
  • 24
  • 4
  • This has worked thank you! Is there also a simple way to pass a parameter to the function? – Pman Dec 31 '19 at 13:51
  • Yes! jQuery's .ajax() function supports passing along data to your code, which you can get via a $_REQUEST variable in your test1.php script. I'll update my answer above to include an example. – John P. Jan 01 '20 at 13:28
  • Thanks John that's a great help and works. On the other part you mentioned about getting the values of the radio buttons I do indeed need to do this however the radio button values are ID's obtained from within a while loop for each database record. So every time it loops through, it creates another radio button and assigning the ID to the value for that particular radio button. Therefore the issue im having is it is always taking the last record that is executed - not the one that is actually clicked – Pman Jan 02 '20 at 15:12
  • Hmm, sounds tricky, but you might look into adding some attributes to feed jQuery's selector function, e.g, $('radio.item1'). Between the ID, name, and/or a class you could assign (with some kind of iterator in your while loop), I'd bet you can solve it. Not super clean but it'd work. Best of luck! – John P. Jan 02 '20 at 21:55