I need to pass some data via AJAX POST or GET to a php file which then uses that data to
reference or access a database without actually refreshing the page. How am i to do it?
For ex: if i use:
function callFunc() {
$.post ( " phpFile.php " , { name: "James"}
...
...
...
}
Take a look at Easy Ajax with jQuery should explain things.
When you use Ajax, an http request is made to the server that calls a php script sending post or get variables to this. The script is then executed and the response returned to the main page, with jquery you can manage the response and make the page not to refresh. If you have a HTML form and a submit button the page will be refreshed, you have to avoid this. Use jquery Val() selector to get the input values and send them trought an Ajax call without the submit button.
Related
I have a single page of HTML that includes PHP code.
I am using the PHP to send data from a form to the same page using $_SERVER['PHP_SELF'] as the form action.
There is an isset($_POST... condition in the PHP that detects if the page has loaded with data via POST. If this is true, the PHP compares the value sent to a set maximum value.
If the data is less than the maximum value, a table is displayed.
If the data exceeds the maximum value, I would like to display an error message using jQuery: $('.error_box').append("Error");
The error message is not displaying. I think this is because the PHP is trying to make changes to .error_box before it has loaded.
How can I make sure that the error display function is available, given that I am validating the form data via PHP as soon as the page loads?
You don't have to use javascript or jQuery to add content to '.error_box'
You should add it with PHP directly.
EDIT:
You must know that you can't directly execute javascript via PHP.
Here is how your page is created then rendered (this is simpler than reality) :
Your server receive the request to display a page
The PHP code is executed and output an HTML (+CSS +JS) page
The output is send back to the client who ask for page
The client browser parse the HTML and render it using css rules you specified
The javascript is executed
Wrap it in a document ready function, like so:
$(document).ready(function() {
$('.error_box').append("Error");
});
Edit:
AMDG is probably right also...
I want to send some data to a php function with javascript. I have already post a question about this earlier today here, though I didnt get a satisfactory answer, maybe didnt explain myself right.
So I have some variables in javascript( I get it through some API). I would like to send this variables to a function in php. I am using codeigniter so I call the function like("somecontroller/somefunction").
I would like the send the data through post to the function. The function calls a view and reloads the whole html. I am looking for something like sending a form. Only I have the variables in javascript. If there isnt any other way I will dynamicly create a form with jquery and send the data with the .click method on the submit button. But is there a more elegant solution??
I know about ajax, but the problem is that I dont want a asynchronous request I want to just submit the data and then for the new html to load.
The easiest way I can think to solve this would be to send the data through the query string, although you'd have to change the target page to accept GET or REQUEST instead of just POST.
To do this just:
window.location.href = 'somecontroller/somefunction?var1='+var1+'&var2='+var2;
Take a look at this link how to pass arguments to your controller methods in CodeIgniter. Then just redirect user to this link.
If you want to redirect through the Form, set action of form to your controller and in php you can access your form elements through $_GET or $_POST
Since you said you're using jQuery:
$.post("somecontroller/somefunction", {
param1: var1,
param2: var2,
...
}, function(result) { ... });
So I have the following code:
<div id="currentmotto"><?php mottoGrab($name); ?></div>
And what this does is it uses curl to screen scrape a users motto and display it on the site. What I need it to do is for that function to refresh every few seconds to see if the user has updated the motto.
I know with jquery I can use the .load('phpfile.php') but the problem then is if I put that function in that file, it no longer gets the $name variable as that is from another page.
Any ideas?
Pass name to phpfile.php via the query string:
.load('phpfile.php?name=THENAME');
Then grab the name from within phpfile.php using $_GET['name'] and stick it in the function.
OR
Make an AJAX request passing the name and update the page using javascript.
Since PHP is executed server-side, it doesn't change once the client loads the page. The only way to refresh a div without JavaScript is to reload the page.
I would try using AJAX to get the name from the other file and update the HTML with JavaScript
I have a php page that takes some get strings, after some user interaction I want to continue executing some php code without navigating or refreshing the page so that its smooth and doesn't flicker. Ive tried secretly clicking invisible forms and it always refreshes, how can achieve this?
Also as a side note, I am using jquery but I was not able to get that post function up and running, I will keep trying it but let me know if that is a wrong solution.
//gallery.php
//jquery
$(".download").click(function()
{
$.post("gallery.php", { images: "testing it out" } );
});
<?php
if(isset($_POST['images']))
{
echo "It worked";
}
else if(isset($_GET['artist']))
{
echo "It worked2";
}
?>
They are both in the same page, the get always works because I pass the info though the url from another page. But the .download click doesnt cause the php code under post to execute
Use Ajax to send a HTTP request in the background.
Using Ajax is the solution...
You can try with jQuery, read this link http://api.jquery.com/jQuery.post/
jquery ajax is very simple method for post a form without page refresh ..
read more about ajax
Are you thinking of AJAX?
I am trying to passing javascript variable value to another page and retrieve here into a php variable
Using function i made a pop up to appear and store it to a java script variable so now how do i pass it another page and i do have some text box values but i get this ,i use action and give page name in it.I need both text box value and prompt value in another page.
Please understand JavaScript is ran on the client machine, and PHP on the server.
HTTP is stateless.
You have to use AJAX.
Are you familiar with AJAX?
Use JavaScript to dynamically add hidden form fields into your form and set the value (alternatively just create the hidden fields in the php output with no value and just use JS to set it).
If you use jQuery you can call something like:
$.post("test.php", { name: "John", time: "2pm" } );
See http://api.jquery.com/jQuery.post/
Using JS you can put a variable into cookie:
document.cookie = 'var1=VALUE1'
Then read it on the other page.
Or you can append #var1=value1 to URL, then read it on the other page using
top.location.hash