submit form after correct reply from post - php

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?

Related

Running JQuery only if the page has POST data

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

Remove content of textarea when iframe is done?

So currently my website works like this; you post an update and through iframe your update gets added to the database and then shown in a list below. But the problem is that when you clicked "Submit" the text you wrote is still shown in the textarea because the website doesn't update completely. I have tried to have "onsubmit" and "onclick" but both remove the content of the textarea before it gets added to the database so it displays an empty message.
What should I do in order to delay it just a second or how do I make it wait for the iframe to "send" data to my PHP-script?
Give your textarea an id and supposing that iframe is an element of the page that contains the textarea you have done the following in the iframe page:
<?php
//code should be done after db add
?>
<script>
o = parent.document.getElementById('textareaID');
o.value = '';
</script>
<?php
//the end of code or something else
?>
You are able to see those demos on jsbin:
http://jsbin.com/ulOyiVo/1 The page with iframe. Supply the textarea with any text and then click on simulate submit link
http://jsbin.com/EyuBeLo/1/ The iframe page
If your only problem is to have a delayed response, you could trigger a setTimeout function to your onClick, with the given setTimeout:
setTimeout(
function() {
alert('hello');
},1250 //in milliseconds
);
You can define a click or a submit event using jQuery and send a request to your server. You can handle the event when the server responded using a callback. In that callback you need to do whatever it is needed to do. Using setTimeout in this case is an unnecessary hack. You will either set up a big time to wait harming the user experience or in case the page responds later than the specified time your page will work unexpectedly. So, instead of that try defining an event.

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

AJAX and JS for LIKE Button

I'm trying to learn how make an AJAX script
for a LIKE button, on my website. I have the following questions:
if i'm sending 1 variable.... id.. I do this
data: "action=vote_up&id="+(this).attr("id")",
is this syntactically correct if i'm sending two variables id and id1 ?
data: "action=vote_up&id="+(this).attr("id")&id1="+(this).attr("id1")",
2) What goes into the href attribute? The php page or the AJAX?
<img scr="like.png">
3) which is run first.. The php page or the AJAX.
4) Is it mandatory for me to use jQuery or Pure Javascript for running AJAX
thanks for your time and patience. I most appreciate it.
1) Yes, you could simple undestand it as a PHP-Get request to a script, so multiple vars are possible, like Adam mentioned.
2) For backwards compatibility you should just link to a PHP/whatever-Script that provides the same functionality but doesn't rely on javascript (Not everyone has js enabled). In your javascript you just disable the defult click actione ( see: http://api.jquery.com/event.preventDefault/ ) otherwise it you only want to allow the like funktionality if js is enabled than you could just link to the page anchor '#'.
3) The page runs first. It is progressed by the server and than sent to your browser. In the browser the recieved javascript will start its action.
4) Everything you are using in jquery is based on simple javascript functions, but jquery is much more comfortable ;) The equivalent to the ajax method of jquery is XMLHttpRequest ( http://www.w3schools.com/xml/xml_http.asp )
Here is a idea, hope it helps.
If handle_vote.php is the URL responsible for the handle the up vote, you must do two things:
the a href is the URL with the query string for the up vote, your data, is this case. It must be generated for you server application. It will be used in case of no javascript.
you should put you event to handle the up vote in the a onclick event, to send the ajax request, and use the preventDefault jQuery function to avoid the default event. In this case, a href will never be used, the js will suppress the link click.
A code sample will be almost like this, in you php page:
<a class="like" href="handle_vote.php?action=vote_up&id=<?php echo $post_id; ?>"><img src="like.png"></a>
And it as your jQuery script:
$(function() {
$('a.like').on('click', function(e) {
e.preventDefault();
$.get($(this).attr("href"));
});
});
You can personalize as you like, it is only the idea of how to do it.
<img scr="like.png"> put onclick event on that link, and make AJAX request` to increment count, on success response update count clicks on button. And you forgot about one thing, you should save the state of that button. Because one user can go to your site and click 1000 times on it.

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>';

Categories