0

I am not sure if this is best approach, but how to make AJAX call with included file? Let me make working simple example.

I have, main index.php file, with following content, where I want to access all data returned from included file where magic happens. Data is array type in reality.

<?php
require_once('magic.php');
?>
<html>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
 <input type="text" name="variable" id="variable" />
 <input type="submit" value="Do magic" name="submit" />
</form> 
<?php
 echo "<pre>";
 print_r($data);
 echo "</pre>";
?>
</body>
</html>

And "magic.php" file, where all the magic happens.

<?php
$variable = $_POST['variable'];
function doMagic($variable) {
 # do some magic with passed variable ...
 # and return data
 return $data;
}
$data = doMagic($variable);
# now return $data variable so main file can use it
return $data;
?>

What I want to do is to call this function doMagic() with AJAX, but all logic is already included and available in global space, and return $data to modal or pop-up window.

What is best approach, because I am already passing variable to included file?

asked May 24, 2015 at 22:25

2 Answers 2

1

Use ajax like the following

$('form').submit(function(){
 $.ajax({url: "magic.php", 
 data: {variable : $'#variable').val()},
 success:function(data){console.log(data);});
});

and in magic.php, use json_encode

<?php
$variable = $_POST['variable'];
function doMagic($variable) {
 # do some magic with passed variable ...
 # and return data
 return $data;
}
$data = doMagic($variable);
# now return $data variable so main file can use it
return json_encode($data);
?>
answered May 24, 2015 at 22:31
Sign up to request clarification or add additional context in comments.

6 Comments

Than in that case, I don't have to include magic.php file, because I will pass and recieve data with AJAX? Only reason to include magic.php in index.php is to make functions globally available to index.php, and to submit form without redirection to action page (magic.php).
@AlanKis, exactly, you dont have to inlude magic.php in the first file if you are not using variables/functions from that file. As for submitting without redirect, thats a different story, you need to return false in onsubmit. Let me know if you need anymore help
JS confuses me a little bit, but just for now. Form action is now "<form action="magic.php" method="POST">" I have added return false at the end of submit(), but it still redirect me to blank magic.php file. I will try to make some fiddle somewhere.
here is pastebin link. PHP Fiddle is uselles as we loging data to console. pastebin.com/qSRs2M8T
@AlanKis, put return false in the last line of the submit handler.
|
1

Since you have jQuery as a tag...

You could do something like this:

In your index.php:

<input type="text" id="variable">
<div id="#myButton"></div>
<div id="#response"></div>

Then have a Javascript file (or include in your index.php):

$('#myButton').click(function(){
 var myVariable = $('#variable').val();
 $.post( "magic.php",{variable: myVariable}, function(data) {
 $( "#response" ).html( data );
 });
});

This basically means, when you click the item that has the id of myButton, it'll grab the value of what's inside of the input with the id of variable, POST that via jQuery.post() to your magic.php file, and return the data. Then, it replaces the html of the div with the id of response with the data it returns.

Just make sure to include the jQuery library

answered May 25, 2015 at 2:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.