Jump to content

Get data from textbox and redirect to another site attaching the textbox info


mandan

Recommended Posts

I am looking for php solution and not for javascript solution for the following:

 

The form has one textbox (type=text name=firstName) and one button (value=Search, type=submit). The user enters the data and clicks on 'Search' button.

 

Once the 'Search' button is clicked,

1.

The code must verify that the length of the string entered in the textbox is greater than 1 ( > 1).

2.

If textbox string is greater than 1, do the following:

a new window is opened (target="_blank" ??) and the URL will be for e.g. "http://mouse.ca/search/eatable/books/find?settings=silent&query="+TheDataThatWasEnteredInTextBoxFirstName

3.

If textbox string is less than or equal to 1 (<=1). Nothing happens. (The user is on the form page with one textbox and one button with whatever he/she entered in the textbox)

 

PHP please, NO javascript.

 

Please help.

Thank you.

 

Link to comment
Share on other sites

I am not familiar with php as I have been using .net for too long. If a new window cannot be opened, let that condition be omitted. I would like to know the detailed steps / code on how to grab the info. in the textbox, check it for one or more characters and add the URL part and redirect it to the mentioned URL along with the textbox input. Once again I am a beginner and a detailed code/explanation will be helpful.

 

Thank you.

Link to comment
Share on other sites

Data submitted by a form can be accessed via the superglobals.

 

$_POST and $_GET

You access the data via the name attribute of the field being sent, in your case it would either be:

$_POST['firstName'] or $_GET['firstName']  (since you didn't specify your method).

This is a small example tutorial.

 

To check the length of a string, use the strlen function.

 

In PHP, you cannot "do nothing" when a form is submitted, and I assume likewise in .NET.  The "do nothing" event would be handled by javascript intercepting the submit and acting accordingly.  You can however direct the application flow after submission based on whether it meets your requirements (ie strlen >  1).

 

That part is just common logic statements:

if(strlen($_POST['firstName']) > 1) {
//do something
}else{
//could show the form again, with fields filled in, or an error
} 

As for redirecting, you can use header to redirect as long as nothing (NOTHING) has been output to the page... you can also append your firstName data perhaps more confidently when cleaning it with urlencode and maybe htmlentities.

$fn = htmlentities(urlencode($_POST['firstName']));
header('Location: http://www.examplesite.com/query=' . $fn);

Please look into the functions in the manual as they will help out a lot.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.