How to get form results to be processed into the url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hanniella
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2008 3:54 pm

How to get form results to be processed into the url

Post 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.
User avatar
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

Post 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.
User avatar
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

Post 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;
  }
?>
hanniella
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2008 3:54 pm

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

Post 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
hanniella
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2008 3:54 pm

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

Post 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.
User avatar
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

Post 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.
Post Reply