-1

I'm having trouble trouble trying to call a function from a html button. I've got this code:

HTML:

<input type="button" value="Do it" onClick="sv()" />

PHP:

function sv(){
$sqlqueryadd = "UPDATE general_list SET nes=2 WHERE nes=1 AND id=100";
$sqldoadd = mysql_query($sqlqueryadd) or die(mysql_error());
}

JS:

function sv() {
var x="<?php sv(); ?>";
alert(x);
return false;
}

This obviously doesn't work, only alerts the "sv();" code.

Thanks a lot!

Jason Roell
  • 6,473
  • 4
  • 18
  • 26
Tak-MK
  • 26
  • 1
  • 9
  • 3
    you cannot execute php code from javascript as php is server side and javascript is client side. to execute a php script you would have to make an ajax call to that php script. Also note even if you were able to do the call the way you have it your php `sv` function does not return anything and you do not echo anything in the `` part so you wouldnt get anything anyway. – Patrick Evans Feb 13 '14 at 22:19
  • `var x="";` why on earth would you do this – meda Feb 13 '14 at 22:20
  • 1
    Be aware that PHP code only exists while your page gets generated by the server. JS code only exists while the page is viewed by browsers. If you want to run a function from your button, then make the PHP generate the javascript function body. In this case, however, you want to learn about writing a decidated php script that acts as an API, so that you can perform ajax requests on it and get data flow between your page as it's being viewed on someone else's computer, and your server. – Mike 'Pomax' Kamermans Feb 13 '14 at 22:20
  • I suggest you look at jQuery and AJAX -- http://api.jquery.com/category/ajax/ -- it's pretty easy with jQuery. It's not straight forward if you try to write it yourself. – Alexis Wilke Feb 13 '14 at 22:23
  • I see... thanx a lot! There isn't any way to do this without ajax? I mean, send a mysql query. – Tak-MK Feb 13 '14 at 22:24
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – John Dvorak Feb 13 '14 at 22:25

3 Answers3

1

The PHP can't be run like that. It is run on start up. You will need to make the PHP on a separate page and AJAX it.

So use something like

function sv() {
 $.ajax({
        url: "sv.php",
      }).done(function(data) {
        alert(data);
      });
return false;
}
Patrick Eaton
  • 706
  • 3
  • 11
  • It didn't work :( I changed the url to ../php/sv.php (I have the js in a "js" folder), and the code in php is: – Tak-MK Feb 13 '14 at 22:40
0

onClick attribute execute only javascript code. From JS you can't execute any PHP functions. You need to send AJAX query to the server an handle them with PHP and return back the result. Or simply try to send html form with submit button in the form:

<input type="submit" name="sv" value="Do it" />

and handle this form data with PHP

Victor Bocharsky
  • 11,090
  • 12
  • 55
  • 88
0

This will do the trick

<input type="button" value="Do it" onClick="javascript:sv()" />

insert "javascript:" before the function call.

then call the php script with this:

$.post("sv.php", { var1: var1Val, var2: var2Val}, function(result) {
    // do something with the data php echo's back
});

the above code uses jQuery.

vero
  • 136
  • 1
  • 5
  • I have to add any variable for the mysql query? Can I skip that? – Tak-MK Feb 13 '14 at 22:42
  • If you'd like to pass variables from javascript into the PHP script just change "var1", "var2" etc to the name: value pairs which will be posted to the script. in your PHP script you'll access them via $_POST['var1'], $_POST['var2']. – vero Feb 14 '14 at 16:56