Jump to content

accessing submited form values, error. help!!


jaboc

Recommended Posts

I keep getting this error  PHP Notice: Undefined index: search in simpleformdata2.php on line 12

line 12 is the line where the search variable is declared. I am still learning php and this is code from a text book. coppied line for line.

<?php

$search = $_GET["search"];

$self=$_SERVER['PHP_SELF'];

if ($search != NULL )

{

('

<form action="'.$_SERVER["PHP_SELF"].'" method="GET">

<label>Search: <input type="text" name="search" />

</label>

<input type="submit" value="Go!" />

</form>

');

}

?>

Link to comment
Share on other sites

What is this chunk of code?

<?php
('
<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
<label>Search: <input type="text" name="search" />
</label>
<input type="submit" value="Go!" />
</form>
');
?>

 

It looks like you're missing an "echo"

<?php
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
<label>Search: <input type="text" name="search" />
</label>
<input type="submit" value="Go!" />
</form>';
?>

 

Ken

Link to comment
Share on other sites

hmm I don't think this is gonna work.

the moddified code you gave me didnt include the if statement which is used to check if there is data in the text field.

I tried this

<?php

echo '$search = $_GET["search"]';

$self=$_SERVER['PHP_SELF'];

if ($search != NULL )

{

('

<form action="'.$_SERVER["PHP_SELF"].'" method="GET">

<label>Search: <input type="text" name="search" />

</label>

<input type="submit" value="Go!" />

</form>

');

}

?>

got this error

$search = $_GET["search"] PHP Notice: Undefined variable: search in simpleformdata2.php on line 14

when I tried this

<?php

echo '$search = $_GET["search"];

$self=$_SERVER['PHP_SELF'];

if ($search != NULL )

{

('

<form action="'.$_SERVER["PHP_SELF"].'" method="GET">

<label>Search: <input type="text" name="search" />

</label>

<input type="submit" value="Go!" />

</form>

');

}'

?>

this error came up 

PHP Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in simpleformdata2.php on line 13

Link to comment
Share on other sites

I am confused because I read the guidelines for posting and it said that this is a forum for people learning php yet I ask for help and I am told that I should go read tutorials before I post. also in the guidlines it says people who are new to php should post in the php newbie forum which I couldnt find.

Link to comment
Share on other sites

The php newbie forum hasn't existed on this site in over a year. The guidelines need to be changed.

 

I just posted one piece of your code that I felt was in error. Now I will post your entire code with the fixes that should make it work (maybe) :)

 

<?php
<?php
$search = $_GET["search"];  // you don't want an "echo" here since this line needs to be executed
$self=$_SERVER['PHP_SELF'];
if ($search != NULL )
{
     echo '<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
              <label>Search: <input type="text" name="search" />
              </label>
              <input type="submit" value="Go!" />
              </form>';
}
?>

 

Please remember to surround all of your code with


tags.

 

Ken

Link to comment
Share on other sites

That is just a "Notice", not an error. Looking at the code, the logic is incorrect. Try this:

<?php
if (!isset($_GET['search']))
{
     echo '<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
              <label>Search: <input type="text" name="search" />
              </label>
              <input type="submit" value="Go!" />
              </form>';
}
else
     echo 'You want to search for <span style="color:red;font-weight:bold">' . stripslashes($_GET['search']) . '</span>';
?>

 

I put the "else" clause in so you will see what you entered into the form.

 

Ken

Link to comment
Share on other sites

I am confused because I read the guidelines for posting and it said that this is a forum for people learning php yet I ask for help and I am told that I should go read tutorials before I post. also in the guidlines it says people who are new to php should post in the php newbie forum which I couldnt find.

 

allow me to clarify. we are not here to re-invent the wheel. there are plenty of tutorials on the internet explaining just about everything you could want to know. 'just about' are the operative words in that sentence. after doing research online and the manual, you should post your questions here. you were looking for a how-to 'if/else' condition... this is a very elementary procedure that can be found in a thousand other places. please, don't waste the space on simple posts. come here to acquire conceptual and procedural understanding of things you do not yet know, but only after you have attempted on your own.

 

unfortunately, that book of yours isn't a very good reference. replace that text book with those two tutorial sites i posted, and you'll be good to go. welcome to the forums!

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.