Exectue php function on submit button

Okay… i have searched the internet for over an hour and i still can’t do this the right way :frowning:

I have a simple button in a php page and i want it to run a php function when pressed. I tried using




But the result is that the page gets reloaded and i have an error on xampp. I don’t want the page to reload…i just want a simple function to be executed…


I’m just a newbie so might be doing everything wrong, but does anyone have an idea about my issue?


Thanks…:slight_smile:

Hey Ruben you might wanna do something like this:




PHP code

<?php

if(isset($_POST['go']){ // button name
do_stuff();
}else{
do_other_stuff();
}

?>

Hope that’s what you want

Thanks! It worked! :slight_smile:

You’re welcome, if you need more help that’s what we’re here for :wink:

I managed to do this but i need to do more… what about executing a php and a javascript function in the same time?


I basically want the button to do these two things:

a. Create a new file(php)

b. Add a line in a table(js)


I managed to do both, the php function in the way that you described and the js function with onsubmit and innerHTML. Is this the right way? Or should i call the php script from js somehow?

You want to create a php file using code ?
If you want to call a function in a php file you should use ajax to do that or call it from another php script

tell me what exactly you want to do

I want to create a simple xml file using php code. I also want to notify the user that the file is created so i am also running a js function that changes the html content…


How can i call a php function using ajax?

You can call a php function using js like this i’ll show you the exaple with jQuery:

$.ajax({
type: 'POST',
url: 'submit.php',
data: {'action': 'do_stuff', 'id' : 1 } // you can use as much as data you want to send,
dataType: 'JSON', // so you can use the json_encode php function
success: function(response) {
if(respons.success = 1) {
show_a_success_message_to_the_user
}else{
show_an_error_message_to_the_user
}
}
});

for the php file

<?php
$post = $_POST;
if(isset($post['action']) && $post['action'] == 'your_wanted_action' ){
if(do_stuff()){
echo json_encode(array('success' => 1, 'message' => 'your success message'));
}else{
echo json_encode(array('success' => 0, 'message' => 'your error message'));
}
}
?>

ihope that helped and you’ve been able to understand it

Thanks! I’ll look into it :wink:

Ruben, I just wanted to chime in and maybe help you grasp an important distinction in the server/client, PHP/JS paradigm, if I may.

Every bit of code on your site has to run someplace. There are two possibilities for that location:

  1. Server-side - this means that the code runs on the server, before the page is ever sent to the user

  2. Client-side - this means that the code runs in the browser, on the end-user’s computer (after the page has been generated by the server and loaded by the browser)

Understanding this distinction and wrapping your head around what code runs on the server and what runs in the browser is crucial. It gave me trouble when I was first figuring this stuff out, too :slight_smile:

So, PHP runs on the server, and Javascript runs in the browser. They can’t interact directly. Basically, javascript has the capability of sending a request to the server, which can be handled by PHP code, and can send information back to the browser, which can be used by the Javascript (that’s AJAX). On the other end, PHP can write inline javascript dynamically to be executed when the browser loads.

In your original example, that doesn’t work because do_stuff() is executed before the page is loaded in the browser (it’s executed server side), while the action attribute specifies the location to which the form should be submitted. Instead, that location (URL) should be a PHP script that handles the request and can call do_stuff after the form is submitted (that’s the solution iGhost gave you).

Anyway, I just think that’s an important distinction to grasp, and it’ll go a long way toward solving these types of problems for you in the future. Hope that helps clarify things for you - it’s an “Ah ha!” moment for many developers :slight_smile:

I just started using php&ajax so sevenspark i’m i doing it the right way :slight_smile:

iGhost said

I just started using php&ajax so sevenspark i’m i doing it the right way :slight_smile:

Hey iGhost, yup, your code looks good to me - definitely the right strategy. I think jQuery is the easiest way to go as well - it greatly simplifies the AJAX process. I’d make 2 minor suggestions:

  1. This line has some errors (probably just simple typos):
if(respons.success = 1) {

should be

if(response.success == 1) {
  1. After echoing in the PHP, you should probably include die() so that the PHP doesn’t write any additional lines (like whitespace after the closing PHP tag) back to the browser, which can create json or headers errors.

Of course, you’ll also need to attach the AJAX function to a submit event, something like:

$('form').submit(function(){
  //do your ajax here
  return false;
});

It would also be good to include fallback functionality for graceful degradation when JS is disabled. The form could submit hidden elements for action and id. The jQuery could submit an extra data value that indicates that AJAX is being used. This value could be checked server-side to determine whether the server should respond with JSON or not. Obviously the details are specific to the implementation here, but that’s something else to keep in mind.

Also, you can use $.getJSON in many cases, which simplified the ajax call. See http://api.jquery.com/jQuery.getJSON/

Hope that helps :slight_smile:

Ya i tried to edit my posts to fix those typing errors but it wouldn’t work
i also forgot that return false :slight_smile:

Thanks for the heads up :wink:

iGhost said

Hey Ruben you might wanna do something like this:




PHP code

<?php

if(isset($_POST['go']){ // button name
do_stuff();
}else{
do_other_stuff();
}

?>

Hope that’s what you want

How could that work? The second bracket of the if() statement isn’t there, so it would throw an error. :smiley:

I finally had some time to work on this and read more on the topic…and i’ve done it trough jquery:

jQuery.get('add_xml.php?id='+xml_count, function(data){
			
//stuff here
			
});

Thanks for your replies guys! :slight_smile: