Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 11th, 2004, 03:15 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default Notice: Undefined variable: comments/ Form reset

Hello to all,

been having a major problem with this error(Notice: Undefined variable: comments), seem to get it all the time...

Also can anyone please help me understand how to keep data that was entered onto a form present while validating during a submit.

As soon as I enter my info into a form that I have created in php and hit submit, the form would be cleared eventhough the data is not accepted because it didn't pass validation criteria... How do i keep the data present until the user has submited the correct info required on the form ?


thank you
Reply With Quote
  #2 (permalink)  
Old November 11th, 2004, 03:25 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hey there,

This should be pretty easy.

Just echo the $_POST or $_GET variables as the value attributes of the form fields like this:

Code:
<?php
function printSession(arrid)
{
    echo (isset($_SESSION[arrid]))? $_SESSION[arrid] : "";
}
?>
Username: <input type='text' name='username' value='<?php printSession('username'); ?>'/><br/>
Something else: <input type='text' name='otherfield' value='<?php printSession('otherfield'); ?>'/>
HTH,

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
Reply With Quote
  #3 (permalink)  
Old November 12th, 2004, 12:16 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Snibs,

Thanks ever so much for the assistance...

I am still having a problem, with

Notice: Undefined variable: (alot of them might i add) and also its not keeping the data that I have entered on the form when it doesn't meet the validation criteria...

Although it displayes the following messages in each input box...
<br /><b>Notice</b>: Use of undefined constant arrid - assumed 'arrid' in <b>c:\inetpub\wwwroot\gs\product_entry.php</b> on line <b>13</b><br />....

How do i get rid of that...?

again thanks for your assistance
Dungey
Reply With Quote
  #4 (permalink)  
Old November 12th, 2004, 04:29 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Oops I messed up......

Everywhere in the code I gave you put a $ sign in front of arrid.

arrid should be $arrid

Sorry about that,

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
Reply With Quote
  #5 (permalink)  
Old November 16th, 2004, 01:03 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Snibs, thanks again...

Can you or anyone else tell me what's wrong with my code that it won't pass the value of the input field into the db...

<html>
<head>
</head>
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<?php
function printSession($arrid)
{
    echo (isset($_SESSION[$arrid]))? $_SESSION[$arrid] : "";
}
?>

<form action= "<? $_SERVER['PHP_SELF'] ?>" method=POST>

SID# :<input type=text name=sid maxlength=9 value="<? printSession('$sid'); ?>" >
      <input name=submit type=submit> <input name=reset type=reset>

<? include('db_connect.inc');
    $link_id = db_connect();


    if((!isset($_POST['sid'])))
    {
        echo "Please fill in the requested information";
    }
    else if((empty($_POST['sid'])))
    {
        echo "SID empty";
    }
    else
    {
        $query = "INSERT INTO gs_test SET sid='$sid'";
        $result = mysql_query($query);
        echo '<B>'.$_POST['sid']."'s </B>record has been successfully added!<Br>\n" ;


}
    mysql_close();


?>

</p>

</body>
</html>

furthermore, it gives me the error on line 31 (sql statement, the $sid object as a Undefined variable...)

Thank you...
Reply With Quote
  #6 (permalink)  
Old November 16th, 2004, 02:40 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

In your form action... you're not echoing anything, just saying

$_SERVER['PHP_SELF'];

instead of

echo $_SERVER['PHP_SELF'];

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
Reply With Quote
  #7 (permalink)  
Old November 16th, 2004, 05:15 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Snibs thanks, thats all working now, and i still am having trouble keeping the data up when validating, after hitting submit, it won't keep the data up until the appropriate fields have been filled in...

<html>
<head>
</head>
<body topmargin="0" marginheight="0" marginwidth="0">
<?php
function printSession($arrid)
{
    echo (isset($_SESSION[$arrid]))? $_SESSION[$arrid] : "";
}
?>


<form action= "<? echo $_SERVER['PHP_SELF'] ?>" method=POST>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
      <td width="81">PID # </td>
    <td width="419"><input type=text name=pid maxlength=9 value="<? printSession('$pid'); ?>" ></td>
  </tr>
  <tr>
      <td width="81">Mod # </td>
    <td width="419"><input type=text name=modnum maxlength=9 value="<? printSession('$modnum'); ?>" ></td>
  </tr> <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" colspan="2"><input name=submit type=submit>
        <input name=reset type=reset></td>
  </tr>
</table>
<p align="center">
  <?

include('db_connect.inc');
    $link_id = db_connect();


    if((!isset($_POST['pid'])) || (!isset($_POST['modnum'])))
    {
        echo ("Please fill in the requested information");
    }
    else if((empty($_POST['pid'])) || (empty($_POST['modnum'])))
    {
        error_message( "PID empty") ;
    }
    else
    {
        $query = "INSERT INTO product_info SET product_num='$pid', mod_num='$modnum'";
        $result = mysql_query($query);
        echo '<B>'.$_POST['pid']."'s </B>record has been successfully added!<Br>\n" ;


}
    mysql_close();


?>
</form>

</p>

</body>
</html>


thanks again for all your help...

--dungey
Reply With Quote
  #8 (permalink)  
Old October 17th, 2006, 04:51 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

$_SESSION[$arrid] should be $_SESSION['arrid'] all your problems will be solved if you do this as the $_SESSION is an array and arrid is a key in that array not a variable..

I know I know this is some what late, but I was searching for a problem along the same lines and got part of the answer from this, so I figure others may do also.. best complete it eh :D
Reply With Quote
  #9 (permalink)  
Old December 1st, 2006, 03:38 AM
Authorized User
 
Join Date: Nov 2006
Location: Bhopal, MP, India.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to talk2leo
Default

 Hi
rather than keeping the data into as array

you can use JavaScript in your page which will validate the input
and the data will be on the form untill all the fields r not filled.
if all the fields r filled form will be submitted.
something like this
<html>
<script language=javaScript>
function validateForm()
{
 if(document.form1.pid.value=='')
 { alert("Please fill pid");
   document.form1.pid.focus();
   returm false;
}
//check for all the fields like this
}
</script>
<head>
</head>
<body topmargin="0" marginheight="0" marginwidth="0">
<form "name=form1" action= "<? echo $_SERVER['PHP_SELF'] ?>" method=POST onsubmit="return validateForm();">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
      <td width="81">PID # </td>
    <td width="419"><input type=text name=pid maxlength=9 value="<? printSession('$pid'); ?>" ></td>
  </tr>
  <tr>
      <td width="81">Mod # </td>
    <td width="419"><input type=text name=modnum maxlength=9 value="<? printSession('$modnum'); ?>" ></td>
  </tr> <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" colspan="2"><input name=submit type=submit>
        <input name=reset type=reset></td>
  </tr>
</table>
<p align="center">
  <?php

include('db_connect.inc');
    $link_id = db_connect();


    if((!isset($_POST['pid'])) || (!isset($_POST['modnum'])))
    {
        echo ("Please fill in the requested information");
    }
    else if((empty($_POST['pid'])) || (empty($_POST['modnum'])))
    {
        error_message( "PID empty") ;
    }
    else
    {
        $query = "INSERT INTO product_info SET product_num='$pid', mod_num='$modnum'";
        $result = mysql_query($query);
        echo '<B>'.$_POST['pid']."'s </B>record has been successfully added!<Br>\n" ;


}
    mysql_close();


?>
</form>

</p>

</body>
</html>

i think it will work very well n its a very simple way to do this.
yeah Man i can have syntax errors in javascript so check it
it will work for u

Good Luck


Reply With Quote
  #10 (permalink)  
Old December 1st, 2006, 03:43 AM
Authorized User
 
Join Date: Nov 2006
Location: Bhopal, MP, India.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to talk2leo
Default

In the previouus code i made some mistake.actually put the javascript in <head> tag.n also some in changes in form tag

<html>
<head>
<script language=javaScript>
function validateForm()
{
 if(document.form1.pid.value=='')
 { alert("Please fill pid");
   document.form1.pid.focus();
   returm false;
}
//check for all the fields like this
}
</script>

</head>
<body topmargin="0" marginheight="0" marginwidth="0">
<form "name=form1" action= "echo <? echo $_SERVER['PHP_SELF'] ?>" method=POST onsubmit="return validateForm();">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
      <td width="81">PID # </td>
    <td width="419"><input type=text name=pid maxlength=9 value="<? printSession('$pid'); ?>" ></td>
  </tr>
  <tr>
      <td width="81">Mod # </td>
    <td width="419"><input type=text name=modnum maxlength=9 value="<? printSession('$modnum'); ?>" ></td>
  </tr> <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" colspan="2"><input name=submit type=submit>
        <input name=reset type=reset></td>
  </tr>
</table>
<p align="center">
  <?php

include('db_connect.inc');
    $link_id = db_connect();


    if((!isset($_POST['pid'])) || (!isset($_POST['modnum'])))
    {
        echo ("Please fill in the requested information");
    }
    else if((empty($_POST['pid'])) || (empty($_POST['modnum'])))
    {
        error_message( "PID empty") ;
    }
    else
    {
        $query = "INSERT INTO product_info SET product_num='$pid', mod_num='$modnum'";
        $result = mysql_query($query);
        echo '<B>'.$_POST['pid']."'s </B>record has been successfully added!<Br>\n" ;


}
    mysql_close();


?>
</form>

</p>

</body>
</html>

just check for the suntax errors n everything will be fine

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Notice: Undefined variable error on input form tonyb187 Pro PHP 2 May 11th, 2008 06:58 AM
Notice: Undefined index: username in latinquarter PHP Databases 1 April 18th, 2008 11:37 AM
Notice: Undefined offset: ## Herjan BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 27th, 2005 12:29 PM
Loops and Arrays - Undefined offset notice dkintheuk Beginning PHP 6 September 12th, 2005 11:38 PM
Undefined variable majidnakit Beginning PHP 2 April 16th, 2005 11:23 PM



All times are GMT -4. The time now is 03:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.