Jump to content

Field Checking


IT-Guy

Recommended Posts

What Iam trying to do is teach myself how to get php to check a field, and if the field is empty it returns you to the form saying, '$whatever-field-name Field was empty'

 

So here is my script, the error Im getting is:

Parse error: parse error, unexpected $end in D:\my\website\login\test\username.php on line 56

 

Here is my script, any suggestions on how I fix this?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Username</title>
<style type="text/css">
input {color:#666;}

</style>
</head>

<body>

<table style="width: 100%">
<tr>
	<td>Username </td>
</tr>
<?php
##session_start();
$con = mysql_connect("****","***","***") or die('Could not connect: ' . mysql_error());
mysql_select_db("test", $con);



if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<form method=\"post\" action=\"username.php\">\n";
echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Submit\" name=\"submit\"></td></tr>\n";
echo "</form></table>\n";
}else {

$error = array();


if(empty($_POST['username'])) {
	$error[] = "Your username was empty!";
}
<tr>
	<td><input name="Username" size="10" type="text" /></td>
</tr>
</table>
<p><input value="Submit Form" type="submit" /></p>

</body>

</html>

 

Link to comment
Share on other sites

I took a look, and fixed the code you suggested and now Im getting a parse error on line 42, but there isn't 41 lines of code :(

Any suggestions?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Username</title>
<style type="text/css">
input {color:#666;}

</style>
</head>

<body>

<table style="width: 100%">
<tr>
	<td>Username <br />
	<input name="username" size="10" type="text" /><br />
	<input value="submit" type="submit" /></td>
</tr>
<?php
##session_start();
$con = mysql_connect("****","****","****") or die('Could not connect: ' . mysql_error());
mysql_select_db("***", $con);



if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<form method=\"post\" action=\"username.php\">\n";
echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n";
echo "</form></table>\n";
}else {

$error = array();

if ($field=="") { echo "this field is empty!";}
?>

Link to comment
Share on other sites

just a thought but for the future it might be easier for you to use little qoutes inside of big quotes.. i guess its just all on what your comfortable with?

 

example

 

<?php
echo "Now that i am using double quotes to display this text as html i can use 'single quotes' without a problem";
echo "<input type='reset' name='reset' value='Reset'>";
?>

Link to comment
Share on other sites

Ok, now that I got it fixed, when it actions, it actions to username.php  (which is the name of the document Im using to test this out)

 

Now if you leave the field blank, hit submit, it returns and says, 'This Field Is Empty' but it no longer shows the text box for the username field. - Why is that? :( Anyone know?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<?php
##session_start();
$con = mysql_connect("****","****","*****") or die('Could not connect: ' . mysql_error());
mysql_select_db("login", $con);

if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<form method=\"post\" action=\"username.php\">\n";
echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n";
echo "</form></table>\n";
}else {

$error = array();

if ($field=="") { echo "this field is empty!";}
}
?>

Link to comment
Share on other sites

i could be wrong but you really don't need the

if(!$_POST['submit']){}else{}

at all? i think the site will do what you intend for it to without that code

 

----Edited below---

also don't you need to define the $field because currently it is nothing right?

$field = $_POST['username'];

 

also there is a better way to right this to avoid someone using injection on you.. but i am not 100% sure what it is so im not going to tell you the wrong thing

Link to comment
Share on other sites

<?php
##session_start();
$con = mysql_connect("****","****","*****") or die('Could not connect: ' . mysql_error());
mysql_select_db("login", $con);
$username=mysql_real_escape_string(trim(strip_tags($_POST['username'])));

if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<form method=\"post\" action=\"username.php\">\n";
echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n";
echo "</form></table>\n";
}else {

$error = array();}

if ($username=="") { echo "this field is empty!";}
}
?>

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.