new to PHP and have some questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mamounjamous
Forum Newbie
Posts: 4
Joined: Sat May 19, 2007 6:41 am

new to PHP and have some questions

Post by mamounjamous »

in the name of Allah

Hii all,
it is my first time here, I'm new to PHP and I've got a problem which I don't know how to eliminate it.
I have a page which contains a form, this form submits values to a php code which is contained in the same page.

I'm sending firstName and lastName text values from the html form, but for the first time I load this page, those values will be not visible to the php code "as I understand" ... so I get the following messages for the first time even though the page is working:

Notice: Undefined index: firstName in c:\php\www\projects\friendb\addnew.php on line 7

Notice: Undefined index: lastName in c:\php\www\projects\friendb\addnew.php on line 8


here are the 7th and 8th lines in the php code:

$fname = $_POST["firstName"];
$lname = $_POST["lastName"];


I hope I receive the solution for this problem soon.

thank you

Mamoun J.
Last edited by mamounjamous on Sat May 19, 2007 6:56 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isset() or array_key_exists() or empty() may be of interest.
mamounjamous
Forum Newbie
Posts: 4
Joined: Sat May 19, 2007 6:41 am

Post by mamounjamous »

The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


the values are not in an array my friend, the values are still in the HTML form, thats why the php code can't see them till I press Submit!!
and thats why this problem appears only the first time i open this php page in the web server.

I hope i made the case more clear now 

this is the page code by the way:

Code: Select all

<?php
	require ($_SERVER["DOCUMENT_ROOT"]. "/projects/friendb/config/db_config.php");
	$connection = mysql_connect($db_host,$db_user, $db_password) or die("Error Connecting to DB!!");
	mysql_select_db($db_name, $connection);
	$fname = "";
	$lname = "";
	[b]$fname = $_POST["firstName"];
	$lname = $_POST["lastName"];[/b]
	$flen = strlen($fname);
	$llen = strlen($lname);
	if ($flen > 0 && $llen > 0)
	{
		$query1 = "INSERT INTO friendb (id, fname, lname, email, phone) VALUES (NULL,'$fname','$lname','$email',NULL)";
		mysql_query($query1, $connection) or die (mysql_error());
		echo "One Record Added Successfully";
		$fname = "";
		$lname = "";
	}
?>


<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<br><br><br><br>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" >
  <div align="center">
    <table width="40%" border="0">
      <tr bgcolor="#00FF33"> 
        <td colspan="2"><div align="center"><font color="8" face="Calibri, Calisto MT"><strong>Add 
            New Friend</strong></font><font face="Calibri, Calisto MT"></font></div></td>
      </tr>
      <tr bgcolor="#00FF33"> 
        <td width="47%"><font face="Calibri, Calisto MT">First Name</font></td>
        <td width="53%">[b]<input name="firstName" type="text">[/b]</td>
      </tr>
      <tr bgcolor="#00FF33"> 
        <td><font face="Calibri, Calisto MT">Last Name</font></td>
        <td>[b]<input name="lastName" type="text">[/b]</td>
      </tr>
      <tr bgcolor="#00FF33"> 
        <td>&nbsp; </td>
        <td> <div align="center"> 
            <input type="reset" name="Submit2" value="Reset">
            <input type="submit" name="Submit" value="Submit">
          </div></td>
      </tr>
    </table>
<a href = "add.php"> Go to All Friends List
  </div>
</form>
</body>
</html>

The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ind007
Forum Newbie
Posts: 14
Joined: Fri Dec 01, 2006 12:39 am
Location: India

Post by Ind007 »

Before assigning the values to the variables you need to check whether the Submit button is clicked or the page is loading for the first time so

Code: Select all

If (isset('Submit'))
{
assign the values given in the form
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you were to have this assignment in your code:

Code: Select all

<?php
$array = array();
$my_var = $array['some_named_string_that_has_never_been_given_a_value'];
?>
What do you think PHP is going to do with that? The error notice you are getting is because you are attempting to call a value from the $_POST superglobal array that does not exist or is null. What feyd was telling you is that you need to check isset() to see if the array value is set, or array_key_exists() to see if there is an array index in the $_POST superglobal array or empty() to see if that array index is not only set, but has a value as well.

PS $_POST is an array, as is $_GET, $_COOKIE, $_SESSION, $_SERVER and $_REQUEST.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

the values are not in an array my friend, the values are still in the HTML form, thats why the php code can't see them till I press Submit!!
and thats why this problem appears only the first time i open this php page in the web server.
$_POST is an array. It is a superglobal array. The functions feyd pointed you to are correct. Fist check that $_POST['firstName'] is set. if so, assign it to $fname, otherwise, assign null to it or something.

Code: Select all

<?php

// .. snip

        $fname = isset($_POST["firstName"]) ? $_POST['firstName'] : null;
        $lname = isset($_POST["lastName"]) ? $_POST["lastName"] : null;

// .. snip

?>
mamounjamous
Forum Newbie
Posts: 4
Joined: Sat May 19, 2007 6:41 am

Post by mamounjamous »

in the name of Allah


thank you very much all of you guys, this is really interesting that all $_POST, $_GET, ... things are arrays.

have a nice day all

Mamoun J.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome. Glad we could help.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

mamounjamous wrote: thank you very much all of you guys, this is really interesting that all $_POST, $_GET, ... things are arrays.
Please take the time to read the manual, specifically, the sections marked 'language reference', subsections 'types' and 'variables' (especially 'predefined variables').
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ind007 wrote:Before assigning the values to the variables you need to check whether the Submit button is clicked or the page is loading for the first time so

Code: Select all

If (isset('Submit'))
{
assign the values given in the form
}
Looking for the submit button is not proper. Why? Because it's quite possible to submit the form (normally) without the submit button. Look for more specific data, that must be there or use $_SERVER['REQUEST_METHOD']. You still must use isset()/array_key_exists()/empty() however to detect which fields are there or not just to make sure you don't drop errors.
User avatar
Ind007
Forum Newbie
Posts: 14
Joined: Fri Dec 01, 2006 12:39 am
Location: India

Post by Ind007 »

feyd wrote:Looking for the submit button is not proper.
As the PHP code and the form are on same page, we need to check the whether the page is loading for the first time isn't it? and also user can pass the variables in the url itself. So it is always better to check submit buton otherwise there is no use of giving the name to submit button in the form.
Whatever other things you mentioned are good practices.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ind007 wrote:As the PHP code and the form are on same page, we need to check the whether the page is loading for the first time isn't it? and also user can pass the variables in the url itself. So it is always better to check submit buton otherwise there is no use of giving the name to submit button in the form.
Whatever other things you mentioned are good practices.
It has never been good practice to look for a submit button when it can easily not exist, yet the submission be still correct and not falsified.
mamounjamous
Forum Newbie
Posts: 4
Joined: Sat May 19, 2007 6:41 am

thanks

Post by mamounjamous »

in the name of Allah

Hii all guys
thanks, now it is working very well
drilling for other problems now ... hehe

wish u all a great day
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:It has never been good practice to look for a submit button when it can easily not exist, yet the submission be still correct and not falsified.
Right. I believe theres a bug in IE where submission does not post the button if the button was not pressed. If you want to check for submission, your best bet is:

Code: Select all

if(!empty($_POST))
{
    if(!empty($_POST['foo']) && !empty($_POST['bar']))
    {
        // Only respond to this form's data
    }
    else
    {
        $error = 'All fields must be filled.';
    }
}
You're guaranteed that the fields were posted, but not the button.
Post Reply