Page 1 of 1

How to get form results to be processed into the url

Posted: Mon Apr 28, 2008 4:00 pm
by hanniella
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.

Re: How to get form results to be processed into the url

Posted: Mon Apr 28, 2008 5:18 pm
by aceconcepts
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.

Re: How to get form results to be processed into the url

Posted: Mon Apr 28, 2008 5:26 pm
by flying_circus
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.
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.

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

Posted: Mon May 05, 2008 4:24 am
by hanniella
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

Re: How to get form results to be processed into the url

Posted: Thu May 08, 2008 3:17 am
by hanniella
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.

Re: How to get form results to be processed into the url

Posted: Thu May 08, 2008 3:34 am
by aceconcepts
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:

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