How to take user to new page based on their input to a form field

I have a php file containing the following 2 lines:

<label for="exampleEmailInput">Visit another Event at mydomain.com/<br></label>

<input class="u-full-width" type="text" size="8" placeholder="enter number" id="GoToNumber">&nbsp; &nbsp;<input class="button-primary" type="submit" value="Go to Event">

What I need is that when a visitor to my webpage enters some text (say inputx) in the input, and clicks the button “Go to Event”, they are transferred to the page www.mydomain.com/inputx.php

I need this to work in all browsers (including old ones back to say IE7), in 4 OS’s (Windows, MacOSX, iOS, Android) and on all types of device.

What I am seeking is the code I can add to my PHP page (plus any changes/additions needed to my 2 lines above) to achieve this most efficiently.

Many thanks. Stowman

What if the user enters a page you don’t have?

Scott

That would, I think, produce a “404 Not Found” error, which could be handled by an appropriate page, and that could include the same code as I am seeking here to allow the user to try again.

At least, that was my intention. But first I need the code!!

Well as you’ve named the target page, inputx.php then just use that as the form action line. This page can then take the posted (name,id etc) and validate that a page exists and redirect to appropriate page. Note your input field needs a name attribute and label for attribute should match input id value.

<form action="http://www.mydomain.com/inputx.php" method="post"> 
<label for="GoToNumber">Visit another Event at mydomain.com/<br></label>

<input class="u-full-width" type="text" size="8" placeholder="enter number" name="GoToNumber" id="GoToNumber">&nbsp; &nbsp;<input class="button-primary" type="submit" value="Go to Event">
</form>

Drummin. Thank you for your response, but I didn’t say the target page was inputx.

I said:

I need the target page to be whatever the user enters, followed by “.php” and preceded by “http://my.domain.com

I (foolishly, it seems) gave ‘inputx’ as an example of what a user might have entered.

What is the code to : take the user’s input and use it as the appropriate part of the target URL for the form’s post action?

Well I wouldn’t trust user input to be a valid page name. I would go to a known page, and validate input and use a header to direct to proper page.

What is the reasoning of having the user enter the page name? It doesn’t make much sense from a good UI perspective.

Scott

I do not have a “known page”. I want the user to be taken to the page containing their input if they enter a correct input.

If their input leads to a non-existant page, they will get a 404 Error, and I was going to deal with that there.

(BTW : I understand the principle of “never trusting user input” but this is only meant to be a simple demonstration website which will not be widely used.)

Agreed, Why make them type when they could click?

A simple sitemap with a list would be easier.

Or if you prefer a form, some type of auto-complete select drop-down could be nice.

That way you wouldn’t need to bother about things like if someone entered
../includes/db-cnx.php

How does the user know what is a correct input? I still don’t get the reasoning.

Scott

I want my users to type. I do not want to give them a list or drop-down.

My site will have a constantly increasing set of pages with names such as “1001.php” or “33344.php” (with varying gaps between the numbers, not just 1001, 1002, 1003, etc)

I want my users to enter a number they have been given, and if what they enter is valid they go to that page. As explained in other replies I have given, there will be a technique to deal with invalid entries.

Apart from other factors which make this my preferred way of working, if the user has possession of a valid number, this acts as a sort of simple ‘password’ technique.

In case you’re wondering, there is nothing nefarious or illegitimate about my site. It is a series of pages about Events being held by communal, charitable, etc, groups - one page for each Event - and what I am trying to get is a way to allow them to view another Event’s pages after giving them a ‘success.php’ page when they have successfully clicked through a link to an Event’s page.

I’m confused too. Why not just use your htaccess file and redirect the entry to the appropriate site?

Something like (guessing - it’s been a while - run it through a regex tester)

RewriteRule ^([a-zA-z0-9]+)/?$ $1.php [NC,L,QSA]

So in other words, if the user enters

www.example.com/ollyollyoxenfree 

it will forward to

www.example.com/ollyollyoxenfree.php

I don’t want my users to enter “www.example.com/ollyollyoxenfree”.

I want them to enter “ollyollyoxenfree” and for them to be sent to www.example.com/ollyollyoxenfree.php

Are people telling me it is not possible for the user input of a form field to be included in a form action?

Not at all, it is possible. But it is most likely not the best way to go about this.
How many possible page input values are there?

Unlimited.

Um…ok. If you need to have some tracking code or if the users are entering some special code for the event itself or anything similar, you should add the code to the URL as a query parameter. So, your URL, through a form with the GET method, should end up looking something like

http://www.myeventsite.com/event/index.php?code=inputx

or

http://www.myeventsite.com/event.php?code=inputx

or if you use a form and the POST method, then just use the input field with the name “code” or in Drummin’s example “GoToNumber”.

You can then smartly use the $_REQUEST['code'] or $_REQUEST['GoToNumber'] global variable to show what you want to the user within the event/index.php or /event.php files.

In other words, you shouldn’t use the file name for the parameter of the result you want to show the user. You should use the URL query parameters or a POST field variable to do that. This way, you avoid an unnecessary call to the server for rerouting the user to that “special page” (or the need for some JS routing script). It also avoids the necessity to take the parameter out of the PHP file’s name with some rewrite filter in the web server for use in your program, because, you most definitely WILL NOT have an unlimited number of PHP files…don’t even think about it!

:smiley:

Scott

Thanks Scott. Could you kindly give me the complete code I would need to use Drummin’s example with form and GET.

Please remember that if the user enters 123 they should be taken to www.myeventsite.com/123.php

Sorry, but I am not going to give you any code at all, because you obviously didn’t understand a word I just wrote.

Your goal is simply to show certain content, a certain event, when the user enters 123 or 456 or XYZ, right?

What does it matter what the file name looks like?

Scott

A simple solution would be to point your form always to the same php script, and the script will redirect to the right page.
Something like that :
<?php $val = $_GET['GoToNumber']; if (file_exists("/path/to/your/files/".$val.".php") { header("Location: /".$val.".php"); } else { //Show some 404 } ?>
You can also use javascript to do this. Look here : http://stackoverflow.com/questions/5384712/capture-a-form-submit-in-javascript
Use the code from the first answer, then add a redirection in the body of processForm().

Hope it helps

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.