CodeCanyon

Exectue php function on submit button

10 posts
  • Exclusive Author
  • Has been a member for 2-3 years
  • Morocco
iGhost says

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

2847 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 1000 and 1999 users
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark moderator says

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

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) {

2. 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 :)

10 posts
  • Exclusive Author
  • Has been a member for 2-3 years
  • Morocco
iGhost says

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

Thanks for the heads up ;)

69 posts
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • United Kingdom
bilawal360 says

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. :D

2498 posts Nice Guy
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 200 and 499 users
+5 more
RubenBristian says
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! :)
by
by
by
by
by