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?
Related
I have a page that I would like a block of jQuery code to run ONLY if the page has already been submitted through a form and has POST data. Currently I have the item running using .onload but I want it to only do that in the situation someone has already hit submit. I am not sure what the best way to do this is.
Is there a simple condition check for this? The page, when submitted, calls itself if that makes any difference.
You can not check whether a page has POST data or not via JavaScript. You can do it by PHP and with that you can call JavaScript
<?php
if (!empty($_POST))
echo '<script type="text/javascript">
runJquery();
</script>';
?>
runJquery() is the JavaScript function which will do what you want if their is any POST variable in the page.
You can do this by adding this conditional to your PHP code:
if(isset($_POST['x'])){
...your code here..
}
I have a page that contains an HTML form that submits back to itself once the user clicks a link in a list of returned search results. Once they click the link, the page takes the submitted variables, runs a bunch of searches on various external APIs, parses a bunch of data, and adds a bunch of stuff to the database, then redirects to a new page that has been created from that data.
All the searching and parsing can take up to six or seven seconds; I'd like to be able to show the user a "Please Wait" kind of message while all that work is happening behind the scenes.
Trouble is, I can't show and hide a DIV because it will screw up my PHP redirect if I've already generated output before the
header('Location: ' . $newURL);
command. I've searched around for answers but while there are many that are similar, none of them are close enough to my specific situation that I can hack around them.
I'd be grateful if someone could point me in the right direction.
Updated version which now works, courtesy #Izkata from his comments below:
jQuery("a").bind('click', function() {
jQuery('#feedback')[0].innerHTML = 'Searching, please wait...';
})
Turned out what I needed to do was assign bind a the message to the click of a link, not to 'submit', as submit was looking for form data.
The simplest way I can think of doesn't require the server to do anything:
<div id='wait_message' style='display: none;'>
Please wait while search is in progress...
</div>
...
$$('.links').observe('click', function(e) {
$('wait_message').show();
});
(Event is written using Prototype.js; you should use whatever is appropriate (JQuery/mootools/etc))
Using the example page in the comments, this works - it runs in Firebug, so just putting it on your page somewhere should work just fine.:
jQuery('#newMovieSearchForm').bind('submit', function() {
jQuery('#feedback')[0].innerHTML = 'Searching, please wait...';
})
There's probably a jQuery-way to update the text instead of using innerHTML, but I don't know it - we don't use jQuery here.
You are right, you won't be able to output data to the screen and then try to redirect afterwards using PHP. You could accomplish this by echoing JS:
echo 'Please wait...';
// Time-intensive PHP here
echo '<script>window.location = "new-location.php";</script>';
You can do something like this:
First, take care of output buffering, i.e. you want php to display output as it executes - you don't want it to buffer.
That way, you can output some html that will show a "loading.." sort of thing.
And, will wait for the script to end it's execution.
Once, you are done with php, redirect using a meta tag, something like:
echo '<meta http-equiv="refresh" content="5;URL=\'http://example.com/\'">';
I want to redirect error message to index page so i wrote the following code:
$message="Bad answer, go back to page!";
header("location:index.php?message=".$message);
this code in process page, and i'm fetching data in index.php page like:
<?php
if(isset($_GET['message'])) {
$message=$_GET['message'];?>
<?php echo $message; ?></iframe>
}
else { echo ""; }
?>
i tried to display message in iframe....but i couldn't succeed.
Your echoing the iFrame tags incompletely/incorrectly. Try this
<?php
if(isset($_GET['message'])) {
$message=$_GET['message'];
echo '<iframe>'.$message.'</iframe>';
}
else { echo ""; }
?>
When you are talking about DOM manipulation (without needing to refresh the page), you should know the best, most elegant way is to do that with Jquery / Ajax / PHP.
Use $.get or $.post to send data to PHP and get the returned data (this case : Bad answer, go back to page!)
Get your <div> or <iframe> element and load the returned data inside of your selected element with Jquery.
I don't know if you are willing to do some changes. If you are willing to do more changes, please drop a comment so I will give you a code example about it.
Some Information you must know;
$.get and / or $.post.
.load() to load returned data.
I hope this helps.
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.
Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax