Jump to content


A quick PHP Form to Navigation problem


5 replies to this topic

#1 JackF

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 14 November 2007 - 09:14 AM

I'm creating a guitar tablature website, which i've never attempted to do before.
I organise the pages that contain the specific artists like so: http://www.domain.com/tab.php?artist=a

My problem comes when I was trying to make a quick form so that rather than scrolling through the alphabet so go to, for example, M for Metallica tabs, the users could enter the desired letter into a textfield, hit submit and it would take them to that artist page. For example if you enter M and press submit, you are automatically taken to tab.php?artist=m

I messed around with the code but i'm fairly inexperienced with coding my own php (i usually use tuts).

I was wondering if anyone could help me out please?!

Many thanks
~jack

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 14 November 2007 - 12:52 PM

Well you could do this with either Javascript or PHP, I'll show you how to do it with PHP:

PHP:

<?php

if( isset( $_POST['search'] ) ) {

	if( preg_match( '¬^([a-z0-9]{1})$¬i', $_POST['search_for'] ) ) {

		header( 'Location: /tab.php?artist=' . $_POST['search_for'] ); exit;

	}

}?>

Ok simple wht happens there is:

- Form gets submitted
- We do a preg_match check on the input to make sure it is either a number or a letter which is 1 character long
- If it passes the check you do a header redirect to what they searched for and then kills the script

Hope that helps

Matt

#3 JackF

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 14 November 2007 - 03:09 PM

thanks for the reply Mr Matt.

Could you tell me if this is correct, if not could you correct me?

 <?php
 
 if( isset( $_POST['search'] ) ) {
 
	 if( preg_match( '¬^([a-z0-9]{1})$¬i', $_POST['search_for'] ) ) {
 
		 header( 'Location: /tabs.php?artist=' . $_POST['search_for'] ); exit;
 
	 }
 
 }?>
 <form id="search" name="search" method="post" action="#">
   <input type="text" name="search_for" id="search_for" />
   <input type="submit" name="submit" id="submit" value="Submit" />
 </form>

At the moment, as it is above, it doesn't work. But that's probably my fault.

Many thanks
jack

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 14 November 2007 - 03:45 PM

<?php

if( isset( $_POST['search'] ) ) {

	 if( preg_match( '¬^([a-z0-9]{1})$¬i', $_POST['search_for'] ) ) {

		 header( 'Location: /text.php?artist=' . $_POST['search_for'] ); exit;

	 }

}

?>

<form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
   <input type="text" name="search_for" id="search_for" />
   <input type="submit" name="search" id="search" value="Submit" />
</form>

You had the name of the form set to search instead of the submit button being set to search which is what needs to be :P

Should work for you.

Matt

#5 nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 14 November 2007 - 03:50 PM

That way, or you could just use the get method in the form, that way it will add the ?artist={input} in the url for you.

<form id="search" name="search" method="get" action="artist.php">
   <input type="text" name="artist" id="search_for" />
   <input type="submit" name="submit" id="submit" value="Submit" />
</form>

Notice I changed the method and action in the form tag, and the name to artist in the input tag, though this method is less secure, you would want to do really good validation on the artist.php page.

Edited by nitr0x, 14 November 2007 - 03:51 PM.


#6 JackF

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 14 November 2007 - 05:46 PM

Thanks guys for all input!
I will try out your scipts in full as soon as i can.
And btw, check out my site http://guitarpro.tomkhost.com and tell me what you think of the design and layout. bearing in mind i haven't finished it yet, or uploaded any proper content!

Thanks for your suggestions, I will get back to you on how they work out!

jack





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users