sneakyimp;10753392 wrote:generally when i make a form in php, i have that form submit to itself. i put the form handler at the top with a redirect command when it is successful
if (isset($_POST['submit'])) {
// handle the form submission here
if (!empty($_POST['input'])) {
save_data($_POST['input']);
header('location: success.php');
exit();
} else {
// if validation fails, the form below will be ouput
$err = "You must enter some input";
}
}
<?php
<?
if (!empty($err)) {
?>
<div style="background-color:#ff0000; color:#ffffff;"><?=htmlspecialchars($err) ?></div>
<?
}
?>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method=post>
<input type="text" name="input">
<input type="submit" name="submit" value="submit">
</form
?>
Wow! Mine worked! I am Jason who is working on a business form for a
l-o-n-g and love learning php, but often times smash into a "barrior" or lack of understanding the script / code and when it works I am cheering! Here was my problem that might be just solved. In order for a php valid form the form action was a <?php echo $_SERVER['PHP_SELF']; and if I did place the next page url in the action the form would never STOP the submit button from turning OFF when there is an empty text box. Now that you just showed me the "form handler" at the top I must figure out where to place my query so that the username and password will go into database. Here is what I have that I will attatch what you showed me...will it work...I will try:
Thanks,
Jason Kraft
<?php
session_start();
?>
<!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-Type" content="text/html; charset=utf-8" />
<title>Inks Etc. Member Sign Up One 2012</title>
<style type="text/css">
#loginform {
border: 1 solid #660;
background-color: #FFC;
width: 280px;
}
form {
margin: 5px;
}
label {
display: block;
width:90px;
float:left;
clear:both;
}
label, input {
margin-bottom: 4px;
}
.border_blk {
border: 1px solid #000000;
}
.signup_bg {
background-image:url(../InkCartPictures/InksEtc/signup_bg.jpg)
}
.member {
width: 125px;
height: 25px;
text-align: center;
}
.member_bg {
background-image:url(../InkCartPictures/InksEtc/member_bg.jpg)
}
#reset{
float:left;
}
#submit{
float:center;
}
.next {
float: right;
display:inline-block;
border: 1px solid #333;
background: #CCC;
color:#333;
text-decoration: none;
}
</style>
</head>
<body>
<table style="border: 1px solid #000000; position:relative; left:250px">
<!--< ? php echo htmlentities($_SERVER['PHP_SELF']); ?> -->
<tr>
<td>
<table>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit=""/>
<tr>
<td><img src="../InkCartPictures/InksEtc/InksEtc_Login.jpg" /></td>
<td> </td>
<td style="border:1px solid #000000; text-align: center"><span style="font-size:20px">Member Sign In</span></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input name="username" style="border:1px solid #000000" type="text" size="25" maxlength="15" id="username" value="<?php echo htmlspecialchars($_POST['username']);?>" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" style="border:1px solid #000000" type="text" size="25" maxlength="15" id="password" value="<?php echo htmlspecialchars($_POST['password']);?>" /></td>
</tr>
<tr>
<td>Password :<br />"Confirmation"</td>
<td>:</td>
<td><input name="confirm" style="border:1px solid #000000" type="text" size="25" maxlength="15" id="confirm" value="<?php echo htmlspecialchars($_POST['confirm']);?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit"></td>
<td><a href='../MemberLogin/ie_signup_two.php' class="next" />Next >>></a></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<?php
/*//////////////////////////////////////////*/
/*/// Inks Etc Sign Up ///*/
/*//////////////////////////////////////////*/
// Include DB Connection
include ("../SearchEngine/dbc_site.php");
function check_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($data);
return $data;
}
//$_SESSION['host'] = "Inky";
$_SESSION['username'] = $user_name;
/*In the case of mysql_real_escape_string(), this means that any character that can't be part of a single string in a MySQL
query is going to be turned into a valid one by being prefixed with "\" (MySQL's "escape character" - the character used to
tell MySQL that regardless of what comes next, it's part of the string).*/
//Grab the profile data from the POST
if (isset($_POST['submit'])) {//1
$user_name = check_input($_POST['username']);
$user_password = check_input($_POST['password']);
$confirm = check_input($_POST['confirm']);
$_SESSION['username'] = $user_name;
$_SESSION['password'] = $user_password;
/*Jason this $errorstring is more of a WARING and NOT a permenant HAULT.
How to STOP the submit button before the fields are empty or incorrect is NOT Possible.
But what is possible is too tell the customer what must be corrected and that is
done by making an validation using if-elseif-else statements*/
$errorstring = ""; //default value of error string
if (!$user_name)
$errorstring .= " ● User Name <br />";
if (!$user_password)
$errorstring .= " ● Password <br />";
if (!$confirm)
$errorstring .= " ● Confirmation <br />";
if ($errorstring != "")
echo "<span style='color:#990000'>Please fill out the following fields</span>:<br/>$errorstring";
if (!empty($user_name) && !empty($user_password) && !empty($confirm) && ($user_password == $confirm)) {//2
//Make sure someone isn't already registered using this username
$query = "SELECT User_Id, Username, Password FROM `SignIn` WHERE `Username` = '$user_name'";
//When the username is unique, it will be inserted into the database
$data = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
if (mysqli_num_rows($data)==0){//3
/* Page 337 Head First - basically INSERT INTO is specifying the where the data will be going into the db table; note - NOW () is a function that stands for the data,
and Username and Password is a db column name I have customized and ID is autoincremented so it does not have to be included*/
//$query = "INSERT INTO SignIn (Username, Password, Join_Date) VALUES " . "('$user_name', MD5('$user_password'), NOW())";
$query = "INSERT INTO SignIn (Username, Password, Join_Date) VALUES " . "('$user_name','$user_password', NOW())";
$result = mysqli_query($dbc,$query) or die(mysqli_error($dbc));
mysqli_close($dbc);
exit();
}//3
else { //4
//An account already exists for this username, so display an error message
echo '<div style="color:#990000"> An account already exists for the selected username. Try a new username</div>';
//Clear the $username variable so that the form field is cleared
$user_name = "";
}//4
}//2
//else {//5
//echo '<p>You must enter all of the sign-up data, including the desired password ' . ' twice.</p>';
//}//5
}// 1
mysqli_close($dbc);
?>
</body>
</html>