This should be quite a simple question although I am quite the beginner.
What would the code be to have the results of a form posted into part of the url?
For example if my site was http://www.examplesite.com and the user types fred into the box and presses submit, it would load the page http://www.examplesite.com/fred.php
Any help would be greatly appreciated. Thanks in advance.
How to get form results to be processed into the url
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: How to get form results to be processed into the url
When you submit a form you can submit the form element values in two ways: POST and GET.
POST submits the element values as an array.
GET sends the element values via the url.
What you would need to do is retrieve the textbox value and decide what to do with it.
POST submits the element values as an array.
GET sends the element values via the url.
What you would need to do is retrieve the textbox value and decide what to do with it.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: How to get form results to be processed into the url
In the "action" argument of the <form> tag, point it to an intermediary page that can process form data and redirect to a valid (existing) page.hanniella wrote:This should be quite a simple question although I am quite the beginner.
What would the code be to have the results of a form posted into part of the url?
For example if my site was http://www.examplesite.com and the user types fred into the box and presses submit, it would load the page http://www.examplesite.com/fred.php
Any help would be greatly appreciated. Thanks in advance.
Code: Select all
/* intermediary_page.php */
<?php
$var1 = $_POST['textBox'];
redirect($var1);
function redirect($var1) {
if(validate_post_data($var1)) {
# User has entered a valid page in the form. Go To the page
header("location:http://www.examplesite.com/" . $var1 . ".php");
} else {
# Invalid page entered in form. Go To error page
header("location:http://www.examplesite.com/page_does_not_exist.php");
}
}
function validate_post_data($var1) {
/*
Validation code goes here:
*/
return true;
}
?>Re: How to get form results to be processed into the url
Thanks so much for the help! However I'm still struggling to get this right. I'm probably doing something completely wrong. When you type in your username and click submit, it loads the intermediary page which just shows blank rather than doing the processing.
This is what I have in the form page:
<FORM action="http://examplesite/intermediary_page.php" method="post">
<P>
<LABEL for="username">Username: </LABEL>
<INPUT type="text" id="username" maxlength="100" size="50" value="" name="question:"><BR>
</form></p></b></font>
<input type="submit" value="Send">
I didn't completely understand how to do the intermediary page, what do I put for the validation code? Anyway this is what I have so far:
# /* intermediary_page.php */
<?php
$var1 = $_POST['textBox'];
redirect($var1);
function redirect($var1) {
if(validate_post_data($var1)) {
# User has entered a valid page in the form. Go To the page
header("location:http://www.examplesite.com/users/" . $var1 . ".shtml");
} else {
# Invalid page entered in form. Go To error page
header("location:http://www.examplesite.com/page_does_not_exist.php");
}
}
function validate_post_data($var1) {
/*
Validation code goes here:
*/
return true;
}
?>
I'm sure it's obvious what I'm doing wrong, but if someone could let me know it'd be greatly appreciated! Thanks
This is what I have in the form page:
<FORM action="http://examplesite/intermediary_page.php" method="post">
<P>
<LABEL for="username">Username: </LABEL>
<INPUT type="text" id="username" maxlength="100" size="50" value="" name="question:"><BR>
</form></p></b></font>
<input type="submit" value="Send">
I didn't completely understand how to do the intermediary page, what do I put for the validation code? Anyway this is what I have so far:
# /* intermediary_page.php */
<?php
$var1 = $_POST['textBox'];
redirect($var1);
function redirect($var1) {
if(validate_post_data($var1)) {
# User has entered a valid page in the form. Go To the page
header("location:http://www.examplesite.com/users/" . $var1 . ".shtml");
} else {
# Invalid page entered in form. Go To error page
header("location:http://www.examplesite.com/page_does_not_exist.php");
}
}
function validate_post_data($var1) {
/*
Validation code goes here:
*/
return true;
}
?>
I'm sure it's obvious what I'm doing wrong, but if someone could let me know it'd be greatly appreciated! Thanks
Re: How to get form results to be processed into the url
Hi, if anyone could help with getting form data processed into the url that would be great (see below) I will appreciate it big time.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: How to get form results to be processed into the url
I always think its quite a good idea to put the validation before the form - this way will allow you to throw any necessary error messages.
A good way to check if a form has been submitted is to check if the submit button has been clicked or to check if there are any POST values:
The problem with relying on whether a submit button has been slicked is that a user may simply hit the neter key - like many people do when they use a login form.
So check the server's request method is more reliable.
A good way to check if a form has been submitted is to check if the submit button has been clicked or to check if there are any POST values:
Code: Select all
//Check if submit is clicked:
if(isset($_POST['submit']))
{
//validation here
}
//or
//a more reliable method would be:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//validation here
}
So check the server's request method is more reliable.