Running JQuery only if the page has POST data - php

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..
}

Related

submit form after correct reply from post

index.php:
$.post('example.php',{ id:pin },
function(data) {$("#test").html(data);}
<div id='test'></div>
<form><input...></form>
Using the above i'd hoped to drop a script into example.php page that would cause the page to submit...
example.php:
if($_post['id']==5){
echo '<script>function submitform(){document.getElementById("form").submit();}
</script>';i}
I had planned to add a DOM even listener for mouse movement key press... and then have that fire off the submitform() script... I tried a few things without luck and I know there has to be a cleaner/Working way...
Thanks,
JT
The problem is you're not calling your submitform() function, but since you want to run the code right away I think it's better if you don't use the function at all and just output the code. Also make sure the form has the id attribute.
So if I understood correctly you want to submit a form after having submitted data already?
If so why don't you just submit everything and process it server side only if conditions are met?

PHP onSubmit action on an <a>

So basically my question is very simple, I have two buttons, I for page forward, one for page backwards, If one of those is pushed, a javascript function is called inside an onClick Event. Javascript then gets the variables of the page and then redirects to the next page, the only problem is, that I need to pass those variables to PHP in order to put them into the Database. So for that I make a load of cookies to pass the variables.
However, I was wondering if something like this would work :
<form>
<a onClick="nexpage();" onSubmit="phpScript.php"> <img src = "previous button.jpg"/> </a>
</form>
The idea behind this is that I want to store the variables in a PHP script, which will put them in a display:none; <div> and then for javascript to get the variables out. This instead of using cookies.
So is it possible to run a PHP script to get the variables and when the script is finished to get them, Javascript kicks in to redirect to the next page...
The reason I don't test this at this moment, is that my code is 100% complete, I don't want any sudden changes that maybe won't work at all. Yes I know back-up this and that, but I thought just asking here, maybe someone will know the answer!
Sincerly,
Harmen Brinkman
You can also use onClick = "this.form.submit(); return false;".
There is no any event like onSubmit for link, instead form do have onSubmit event.
Normal Way as OP asked.
<form action = "phpScript.php" method = "POST">
you can use document.getElementById("my_form").submit();
#Dipesh Parmar – Good point. You could also do:
window.onload=function() {
document.getElementById('my-form').onsubmit=function() {
// do what you want with the form
// AJAX POST CALL TO PHP PAGE
// Should be triggered on form submit
alert('hi');
// You must return false to prevent the default form behavior
return false;
}
});
Inspiration by Capture a form submit in JavaScript

php post without form

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?

Redirect to previous page and display error in a alert box

In my project, the first PHP page takes input from user and submits it to the second PHP page. In the second PHP page, the values which come from first page are validated against values from database, using functions. I want to redirect to the first page if any of the values submitted is wrong. Please help me out with the code. If not possible in PHP please mention the code in any other like jQuery or Ajax.
Here is code that does exactely what you want (according to your title):
<script type="text/javascript">alert("Stupid message");history.go(-1);</script>
I don't like this way of working, you'd better to use sessions, or creating one file that displays the form and does the validating.
#M LOHIT
<script type="text/javascript">alert("Stupid message");window.location.href='previouspage';
</script>
This will surely work for you
echo"<script type='text/javascript'>alert('Data Failed');window.location.href='index.php';</script>";
This might help you
echo '<script>alert("your error message"); location.replace(document.referrer);</script>';

how to submit a form and show the result in that page

im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though

Categories