Neutralise
Neutralise

Reputation: 329

Interface php results with jQuery

I have quite some experience with php, but I am very new to using jQuery.

I am hoping someone can give me some example code (preferably) or point me in the right direction for which jQuery functions I need to use, essentially, what I want to do is for a user to submit a form in php and then for the results of that form submit to be displayed on the same page using jQuery (I assume).

Like I said I have very little experience with jQuery, and none doing something like this. So any help is appreciated.

Upvotes: 2

Views: 128

Answers (3)

bileckme
bileckme

Reputation: 104

Well, this answer is not really targeted at the newbie, but if you really want to dig into writing your own custom a PHP/JQuery interface you might want to try something like this:

  • use ajax function name as a php pseudo function name
  • implement jquery and let php echo the output.
  • this can be useful if you want to only write your code in php only
  • not really practical but it works some 90% of the time.

Note sure if there's any library out there that does this implementation but I do have some sample implementation which I use on some custom development projects, maybe you might find some of the insights useful if you are really into writing your own code.

See the following code snippet

<?php
function js_start(){
  return '<script type="text/javascript">';
}

function js_add_jquery($version='1.4.2'){
  return '<script src="https://ajax.googleapis.com/ajax/libs/jquery/'.$version.'/jquery.min.js"></script>';
}

function js_ajax($url, $data, $success){
 return "
 $.ajax({
    url: '$url',
    data: '$data',
    success: $success
  });";
}


function js_end(){
  return '</script>';
}


//Example usage:
// where success is behaviour you want executed on success
$success =
"function(data) {
  $('#page').html(data);
}";

echo js_add_jquery(); // default version 1.4.2 is used here
echo js_start();
echo js_ajax('test.php', 'name=John&surname=Doe', $success);
echo js_end();
?>

Upvotes: 1

AKnox
AKnox

Reputation: 2635

You could place a function on a button that checks for values in input fields and then writes them out in another area.

Your question is a bit ambiguous.

Resources: http://api.jquery.com/val/

Upvotes: 0

Tomm
Tomm

Reputation: 2142

You might want to check out the jQuery form plugin.

http://jquery.malsup.com/form/

The site has loads of examples too.

Upvotes: 3

Related Questions