/    Sign up×
Community /Pin to ProfileBookmark

If Statement That Runs on Form Submit

I have a real simple form where the use enters a word and hits submit. The word is then appended to the end of a url that I have in the code and redirects to the new url. The script I have is working but the only thing I can’t figure out how to do is when it hits submit I want it to run an if statement first that checks the word they entered and if it’s the exact match to a word we have in the if statement it redirects. If it doesn’t match it says a line of text under the text box “Sorry that word is incorrect”. Here’s the working code. Any help would be GREATLY appreciated.

[CODE] <?php
if (isset($_POST[‘bt’]))
{
header(“Location: http://www.google.com/” . $_POST[‘folder’]);
}
?>
<html>
<form id=”form1″ name=”form1″ method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”>
<input type=”text” name=”folder” id=”folder” />
<input type=”submit” name=”bt” id=”bt” value=”Go To” />
</form>
</html> [/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bionoidOct 04.2013 — Hi, if you mean validating the field on the client before submitting then here is an example:

&lt;?php

<i> </i>if (isset($_POST['bt'])) {
<i> </i> header("Location: http://www.google.com/" . $_POST['folder']);
<i> </i>}

?&gt;
&lt;html&gt;
&lt;head&gt;
[COLOR="#FF0000"] &lt;script type="text/javascript"&gt;

<i> </i> function validate(form)
<i> </i> {
<i> </i> switch (form.folder.value) {
<i> </i> case 'word' :
<i> </i> case 'text' :
<i> </i> case 'another' :
<i> </i> //ALLOW FORM TO SUBMIT
<i> </i> return true;
<i> </i> break;
<i> </i> }
<i> </i> alert('Sorry that word is incorrect');
<i> </i> return false;
<i> </i> }

<i> </i> &lt;/script&gt;[/COLOR]
<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;
<i> </i> &lt;form[COLOR="#FF0000"] onsubmit="return validate(this);"[/COLOR] id="form1" name="form1" method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
<i> </i> &lt;input type="text" name="folder" id="folder" /&gt;
<i> </i> &lt;input type="submit" name="bt" id="bt" value="Go To" /&gt;
<i> </i> &lt;/form&gt;
<i> </i>&lt;/body&gt;
&lt;/html&gt;


The code will currently only allow the form to submit if you enter one of these words: "word", "text" or "another"
×

Success!

Help @zkoneffko spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...