Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie needs proofreading help! can't get script to run!

Status
Not open for further replies.

fowlerlfc

MIS
Mar 20, 2002
136
US
Hello,

I know this will probably be a dumb mistake on my part, but I can't get the following script to run. Could someone please proofread it for me and help me out? Also, since I'm learning if you'd be so kind as to critique my work, that would also be helpful.

Here's the script:

<html>
<head>
<title>Workorder submission form</title>
</head>
<body>
<?
//haven't submitted form, so display it
if (!$submit)
{
?>
<table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;>
<form action=&quot;<?echo $PHP_SELF; ?>&quot; method=&quot;post&quot;>
<tr>
<td>Name:</td>
<td><input size=&quot;50&quot; maxlength=&quot;250&quot; type=&quot;text&quot; name=&quot;name&quot;></td>
</tr>
<tr>
<td>Extension:</td>
<td><input size=&quot;4&quot; maxlength=&quot;4&quot; type=&quot;text&quot; name=&quot;ext&quot;></td>
</tr>
<tr>
<td>Email:</td>
<td><input size=&quot;50&quot; maxlength=&quot;250&quot; type=&quot;text&quot; name=&quot;email&quot;></td>
</tr>
<tr>
<td>Issue/Error/Problem:</td>
</tr>
<tr>
<td><textarea name=&quot;issue&quot; cols=&quot;40&quot; rows=&quot;20&quot;></textarea></td>
</tr>
<tr>
<td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Add&quot;></td>
</tr>
</form>
</table>
<?
}
else
{
//includes files
include(&quot;../conf.php&quot;);
include(&quot;../functions.php&quot;);

//setup up an error list array
$errorlist = array();
$count = 0;

//validate name field
if ($name) { $errorlist[$count] = &quot;Invalid entry: Name&quot;; count++; }

//validate ext field
if ($ext) { $errorlist[$count] = &quot;Invalid entry: Extension&quot;; count++; }

//validate email field
if ($email) { $errorlist[$count] = &quot;Invalid entry: Email&quot;; count++; }

//validate issue field
if ($issue) { $errorlist[$count] = &quot;Invalid entry: Issue/Error/Problem&quot;; count++; }

//check for errors in errorlist.
if (sizeof($errorlist)==0)
{
//if no errors found
//open db connection
$connection = mysql_connect($host, $user, $pass) or die(&quot;Unable to Connect!&quot;);
//select db
mysql_select_db($db) or die(&quot;Unable to select database!&quot;);
//generate and run query
$query = &quot;INSERT INTO wo(name, ext, email, issue, timestamp) VALUES ('$name', '$ext', '$email', '$issue', now())&quot;;

$result = mysql_query($query) or die(&quot;Error in query: $query. &quot; . mysql_error());
//echo result
echo &quot;<font size=-1>Update Successful.<a href=list.php>Go back to the main menu</a>.</font>&quot;;
//close db
mysql_close($connection);
}
else
{
//if there are errors, print a list
echo &quot;<font size=-1>The following errors were encountered:<br>&quot;;
echo &quot;<ul>&quot;;
for ($x=0; $x<sizeof($errorlist); $x++)
{
echo &quot;<li>$errorlist[$x]&quot;;
}
echo &quot;</ul></font>&quot;;
}
}

?>
</body>
</html>

Thanks in advance for all of your help!
 
Okay, it's not running. Three things are considered helpful in this context.

1. What, in general terms, is your code supposed to do?
2. What deviation from expected behavior are your experiencing?
3. What, if any, error messages are being generated?
 
Sorry!

1. The script is supposed to display a form if the submit button hasn't been pushed. When submitted, the script connects to a mysql database (the db and tables exist), and inserts data.

2. When run, the script displays a white screen. No Html, no errors, no nothing.

3. See answer 2.
 
Ok, now i will say one thing, what version have you got of PHP?

I'm saying this cause yesterday i installed 4.2.0 in my home computer and there was no error reporting.

some things i'm seeing in yor code:
replace all the vars from the form ex: $submit by $_POST['submit']

in the if, as you can have to show all errors, you should replace if($submit) by if(isset($_POST) && isset($_POST['submit']))

but when you put diplay_errors to on, you will se the errors you are getting. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I've got PHP 4.20.

I just turned display_errors = on.

I now get the following error:
Parse error: parse error in D:\Inetpub\ on line 46

I've checked this line of code here's an excerpt:

$errorlist = array();
$count = 0;

but don't see an error. Any ideas?
 
is that the line that have the error ... or i'm getting blind. I don't see any errors in you code :( Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Okay, I've found part of the problem in the if statements that validate my fields i forgot to put $ in front of my variable names.

Now my form prints and displays the following message:

Warning: Undefined variable: submit in D:\Inetpub\ on line 9

the variable $submit is for my submit button which is defined on the form? Have I done something wrong?
 
Here's my script again. It's still showing an error of:

Warning: Undefined variable: submit in D:\Inetpub\ on line 9

Here's the script:

<html>
<head>
<title>Workorder submission form</title>
</head>
<body>
<?
//haven't submitted form, so display it
if (!$submit)
{
?>
<table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;>
<form action=&quot;<?echo $PHP_SELF; ?>&quot; method=&quot;post&quot;>
<tr>
<td>Name:</td>
<td><input size=&quot;50&quot; maxlength=&quot;250&quot; type=&quot;text&quot; name=&quot;name&quot;></td>
</tr>
<tr>
<td>Extension:</td>
<td><input size=&quot;4&quot; maxlength=&quot;4&quot; type=&quot;text&quot; name=&quot;ext&quot;></td>
</tr>
<tr>
<td>Email:</td>
<td><input size=&quot;50&quot; maxlength=&quot;250&quot; type=&quot;text&quot; name=&quot;email&quot;></td>
</tr>
<tr>
<td>Issue/Error/Problem:</td>
</tr>
<tr>
<td><textarea name=&quot;issue&quot; cols=&quot;40&quot; rows=&quot;20&quot;></textarea></td>
</tr>
<tr>
<td><input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Add&quot;></td>
</tr>
</form>
</table>
<?
}
else
{
//includes files
include(&quot;../conf.php&quot;);
include(&quot;../functions.php&quot;);

//setup up an error list array
$errorList = array();
$count = 0;

//validate name field
if ($name)
{
$errorList[$count] = &quot;Invalid entry: Name&quot;; $count++;
}

//validate ext field
if ($ext) { $errorList[$count] = &quot;Invalid entry: Extension&quot;; $count++; }

//validate email field
if ($email) { $errorList[$count] = &quot;Invalid entry: Email&quot;; $count++; }

//validate issue field
if ($issue) { $errorList[$count] = &quot;Invalid entry: Issue/Error/Problem&quot;; $count++; }

//check for errors in errorlist.
if (sizeof($errorList)==0)
{
//if no errors found
//open db connection
$connection = mysql_connect($host, $user, $pass) or die(&quot;Unable to Connect!&quot;);
//select db
mysql_select_db($db) or die(&quot;Unable to select database!&quot;);
//generate and run query
$query = &quot;INSERT INTO wo(name, ext, email, issue, timestamp) VALUES ('$name', '$ext', '$email', '$issue', now())&quot;;

$result = mysql_query($query) or die(&quot;Error in query: $query. &quot; . mysql_error());
//echo result
echo &quot;<font size=-1>Update Successful.<a href=list.php>Go back to the main menu</a>.</font>&quot;;
//close db
mysql_close($connection);
}
else
{
//if there are errors, print a list
echo &quot;<font size=-1>The following errors were encountered:<br>&quot;;
echo &quot;<ul>&quot;;
for ($x=0; $x<sizeof($errorList); $x++)
{
echo &quot;<li>$errorList[$x]&quot;;
}
echo &quot;</ul></font>&quot;;
}
}

?>
</body>
</html>
 
As of PHP 4.2.0 (I think), register_globals is off by default. Just do as Anikin suggested above, or set register_globals to on in your php.ini file. //Daniel
 
I already have register_globals = on, and I just made the changes that anikin suggested. This resulted in the same error.

Any more ideas?
 
The problem is likely from the line which reads,

if (!$submit)

The error could be caused by one of two things:

1. you are running the script for the first tiime so the variable is not instantiated before you check on it. Try changing the line to:

if (! is_set($submit))


2. You need to be using the $_POST hash. Change the line to read:

if (!$_POST['submit'])

or probably more correctly:

if (! array_key_set (&quot;submit&quot;, $_POST))

 
Look my first post. I said that you should replace the if(!$submit) :)

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top