Stop resubmit on form - forms

I have a simple form on my site that allows users to add comments on photos. However, the form doenst use ajax (like facebook). Instead, it submits the form and refreshes the page. This is fine however, if a user reloads the page, there is an alert that he/she will resubmit the data resulting in two of the same comments. Id like to remove this resubmit without sending the user to a confirmation page. Thanks.
Here is my form:
<form name='form' action='index.php' method='POST'>
<input type='text' name='comment'>
<input type='submit' value='submit' name='submit'>
PHP:
if (isset($_POST['submit'])){
$comment=$_POST[comment];
$time=time();
$id=$_GET['id'];
$put=mysql_query("INSERT INTO comments VALUES ('','$user','$time','$comment','$id')");

Is your PHP script simply re-rendering the page in response to a form post? The standard way to get the behavior you describe is that it should either return a HTTP status of 204 (No content) or 303 (see other, with a redirect to the page you want to show).

Related

Send email when submit button is pressed

I have a really simple form that allows a user to input an email address here:
<form method="post" action="http://www.mydomain.com/page2/">
<input type="email" name="email">
<input type="submit" value="Submit">
</form>
This works correctly and it takes the visitor to www.mydomain.com/page2 when the submit button is clicked.
I am trying to get it to email me this input email address also when the submit button is clicked. I understand how to email using PHP but can the action have two urls?
Or is there a simpler way of doing this?
On /page2/ access the email in the global variable $_POST['email']. And then you can send it to yourself with PHP mail(). Example:
mail('myemail#domain.com', 'Someone submitted my form', 'Their email was: ' . $_POST['email']);
If you are stuck somewhere else, let me know and I can update the answer.
Once a form is submitted, you are no longer on that page. You've navigated away.
The other way you can do this is submit the first action via AJAX, then submit the form naturally to the second destination. I would suggest using jQuery to make your AJAX calls since most of the AJAX code is already there for you to use.
Another option is to have page2 be a php script, and have it perform the two actions once it receives the form data. See: Post to another page within a PHP script
I understand how to email using PHP
Then I would recommend writing some PHP code that sends the email to you.
but can the action have two urls?
No. A web browser can't make two requests at the same time. Which response would take precedence?
Nor does it need to. Now, you have a target already:
http://www.mydomain.com/page2/
Don't you control that page? That would be the page on which you'd put your PHP code for sending an email. If you don't control that page, then you would want an intermediary page. Something like:
sendmailandredirect.php
(Named solely to illustrate intent, you can call it what you like.) What this page would do is send the email, then issue a redirect to your final target. Something like:
header('Location: http://www.mydomain.com/page2/');
In effect, there would be "two urls" but they're invoked in serial instead of in parallel.
If you wanted to keep the code seperate and the action url as /page2/ you could fire off an ajax request on submit to your sendmail handler.

How to send form data and load the resulting page into and iframe

Okay so I have a basic webpage that just load a simple form.
This form submits a text field, a radio button selection. Very simple.
It sends it to a webpage that uses the data and displays a webpage based on the data.
What I am wondering is how can I submit this data to the webpage, which is located on a different server, and display what that webpage would then display inside of an Iframe.
The reason I ask is I just wish to have an iframe that has almost like a header that stays anchored at the top of their resulting webpage with an advertisement.
Hopefully this is clear enough. I just can't seem to find what I am looking for :(
Thanks everyone!
Are you looking for this? The form will targeted to the iframe with the domain http://example.com and also take its post value
<form action="#" method="post" target="iframe_name">
<input type="submit" name="post" value="Post">
</form>
<iframe src="http://example.com" name="iframe_name"></iframe>

Submit button action

<FORM ID='htmlform' action="" onsubmit="return valforms(this)">
.....
<INPUT TYPE="submit" NAME="submit" VALUE="Submit">
When the user is done filling out the form, and clicks the submit button, I want all of the form information to be sent to me. How do I do this? This is only part of the code, the whole code is too long for me to type in here.
To reply on your question:
Oh, so like the page that emails the data is that an html page that I can have a message saying, "Thank you for your interest, you will hear back from us soon." and then have the page redirect to the home page again? I hope I'm not getting too complicated with this
You're almost right. Let me explain it to you:
the user gets a HTML page that was made in PHP, ASP,.. to fill in
some data in a form
the user fills in the data and clicks a button
The action on the button tells the server to process the page, or have another page to process it. Let's call this page PageX
PageX (written in PHP, ASP,...) will email the data to you
You can also have pageX return some text to the user's browser saying "thank you for your interest,....". You can also make a redirect to another page from this page
Does this answer your question?
The action part of the form element tells the browser what URL to post the information to. You would need to specify some page with some server-side code that would take that information and store it or send it in an email. The onsubmit part of your form element fires a JavaScript event that can be handled on the client's machine. You cannot do much on the client's machine without sending the data back to the server.

Input Button as SUBMIT

I need to have a form submitted using the enter key, however, I have to use a BUTTON instead of SUBMIT as the type in order for the page to not refresh. How can I get my BUTTON to act as a SUBMIT and be executed whenever someone pushes their enter key?
<form>
<input type=text ...>
<input type=button ...>
</form>
A lot of the information I found about this mentions Netscape/IE/lots of outdated material.
This is my HTML output, I'm looking to hide the submit button and use ENTER:
http://i.stack.imgur.com/Ohepe.png
with Javascript enabled
<input type="button" onclick="this.form.submit()" ... />
should work
I have to use a BUTTON instead of SUBMIT as the type in order for the page to not refresh
Nah. Use a normal submit button that refreshes the page. (And ideally, for accessibility, make it work!) Then add progressive enhancement to replace the submission action of the form with something smoother when JS is available. Use return false (or event.preventDefault() in the DOM 2 Events model) to stop the form submitting in this case.
<form id="foo" method="POST" action="dosomething.script">
...
<input type="submit" value="Do something"/>
</form>
document.getElement('foo').onsubmit= function() {
beginAJAXSubmission();
return false;
};
Catching the submit event of a form is generally better than trying to pick up click on buttons, because it will always fire when the form would normally be submitted, including on Enter keypresses. click on the first submit button in a form will usually be fired on an Enter keypress, but there are cases (depending on number of controls in the form and what browser it is) where it doesn't happen and so you can end up falling through to actually submitting the form.
as other said, you have to use Javascript. I recommend JQuery framework.
But i don't understand the refresh thing?
Normal way is you hit submit and your form will be sent over a request to the server.
Server process the data and return a response (HTML/JSon..etc) this response will normally be redirect to a result page (to avoid the famous warning about re-post on refresh).
Now if your form is only a little piece of a bigger page, you might want to use ajax to post the little form and then take the result and update your DOM.
All this said, nothing prevent you to use submit type for the button, it is actually the best way to make your enter key defaut to this action. All you have to do is to use Jquery and intercept the submit of your form and make an ajax call instead of going the normal way.
you will find plenty of example to use JQuery since its probably the most used javascript framework.
Hope it help

Tomcat FORM based authentication, on every page

I would like to use authentication form on every page (in the header of the page), so user could authenticated from any page.
I'm using Tomcat's FORM based authentication, but when i go to my index page, and try to login using the form in the header, I get:
HTTP 400 - Invalid Direct Reference To Login Page
Is there any workaround I could use, some settings in web.xml maybe, to solve this problem?
Edit:
Here is the header's login form:
<form method="post" action="j_security_check">
<input type="text" name="j_username" class="login"/>
<input type="password" name="j_password" class="login"/>
<input type="submit" value="Login"/>
</form>
The concept of Tomcat's form based authentication is that it intercepts unauthenticated requests to protected pages and internally redirects to a separate login page. It also saves the original page request. Once the user has successfully logged in, the original page request is replayed and the protected page is displayed.
What you are trying to achieve is - as far as I can derive from your short description - something different. Basically, all your pages are public. Optionally, a user can log in and will then get personalized pages.
Tomcat won't help you implementing this. When you use the j_security_check action, it will be unable to do anything because it never intercepted a request in the first place.
Instead, it's probably easier to check the username and password yourself if the login form is submitted. If they are okay, just put the username into your session data. An authenticated session can easily be recognized as it contains the username in the session data.
There is probably even a way to reuse the Tomcat realms that are e.g. able to check username and password against a database. But I don't know for sure.
Are you stated your login screen as "welcome" ? In case of success login it tries to loop you through login page again. What is welcome-page tag in your web.xml ?