Help with PHP

th3rmite

Limp Gawd
Joined
Aug 7, 2001
Messages
378
I am hoping that someone here can help me. I know absolutely nothing about php, but I am trying to make a text box on my page where a visitor can input text, hit enter, and the php script will redirect to an address similiar to "http://somepage.com/blog/index.php?s=USERINPUT&cat=6"

I'm trying to make a workaround on the search bar for a wordpress page so that I can search specific categories.

Any pointers to some good info woud be most appreciated.
 
PHP:
<?php
  if (isset($_POST['url'])) {
      header('location:http://somepage.com/blog/index.php?s=' . $_POST['url'] . '&cat=6');
  } else {
      
?>
<form action="<?php
      echo $_SERVER['PHP_SELF'];
?>" method="post"><input name="url" type="text" /><input name="submit" type="submit" /></form>
<?php
  }
?>



to break it down:
PHP:
  if (isset($_POST['url'])) {
This first part checks if a form has been submitted with a url field present. If it has this is executed:
PHP:
 header('location:http://somepage.com/blog/index.php?s=' . $_POST['url'] . '&cat=6');

PHP:
}else{
if a form hasnt been submitted or url field isnt present then execute below
PHP:
?>
<form action="<?php
      echo $_SERVER['PHP_SELF'];
?>" method="post"><input name="url" type="text" /><input name="submit" type="submit" /></form>
<?php
  }
?>
The last part is the code that is executed if no form submission is present. It then breaks out of php (?>) and displays the form, entering into php again to echo the current pages address in the form action field, and then entering php again to close the if statement. I could have just echoed out the last part and made it easier on both of us, but its late and im tired :)
It should be noted this is just a basis, no error checking takes place...
 
Being that I'm not sure exactly how to incorporate what you wrote above, I have figured out halfway on what I want to do:

<form action="./blog/index.php" method="GET">
Enter your search: <input type="text" name="s" />

<input type="submit" />
</form>

This will cause the browser to go to "http://somesite.com/blog/index.php?s=SEARCHTERM"

What I want to do is also add the following text to it "&cat=6"

So that it goes to "http://somesite.com/blog/index.php?s=SEARCHTERM&cat=6"

There has got to be a simple way to do this.
Thanks for the help.
 
Well I spoke before I thought I suppose. I figured out the code on my own, thanks for the help thunder. I am going to use :

"
<form action="./blog/index.php" method="GET">
Enter your search: <input type="text" name="s" />
<input type="hidden" name="cat" value="6" />
<input type="submit" />
</form>

"

It redirects to the page "http://someblog.com/blog/index.php?s=SEARCHTERM&cat=6"

Any tips? Am I to understand that GET has a 100 character limit? How could I work around this?

Thanks in advance
 
th3rmite:
You could use POST instead of GET. Also don't forget to check the data that's submitted for validity. If you don't, someone could pass malicious code/scripts to your website and cause havok.
 
Back
Top