Hi guys I'm new to coding and loving every minute of it :)
So the following code is in my registration.php file. I want to make it so that when the user fills everything BEFORE it will direct them to my invoice.php file after pressing the register button. If they are missing some requirements, go back to the registration form and (hopefully after I get this figured out, put some sticky forms so they don't have to type in whatever text was validated) Also, getting an error "
Notice: Undefined index: submit in C:\xampp\htdocs\assignment_2\registration.php on line 9" on my validation at the top of my PHP code, not too sure what I'm suppose to put there. :( As always, any help is greatly appreciated!
<html>
<h4>
<center>
New User Registration
</center>
</h4>
<body>
<?php
if($_POST["submit"]=="login")
{
if(preg_match("/^[0-9a-zA-Z_]{5,}$/", $_POST["user"]) === 0)
$errUser = '<p class="errText">User must be bigger that 5 chars and contain only digits, letters and underscore</p>';
// Password must be strong
//if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pass"]) === 0)
if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pass"]) === 0)
$errPass = '<p class="errText">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</p>';
// Email mask
if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\#\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
$errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)#chars(.chars).chars(2-4)</p>';
}
?>
<form action="invoice.php" method="post">
<center>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>Username</td>
<td>:</td>
<td <input name="user" type="text" size="16" value="<?php echo $_POST["user"]; ?>">
<?php if(isset($errUser)) echo $errUser; ?>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="password" size="16" value="<?php echo $_POST["pass"]; ?>">
<?php if(isset($errPass)) echo $errPass; ?>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" size="50" value="<?php echo $_POST["email"]; ?>">
<?php if(isset($errEmail)) echo $errEmail; ?>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type='submit' name='register' value='Register'>
</center>
</form>
</body>
</html>
The problem here is one that many programmers that are new to PHP run into. See, $_POST is an array that includes all parameters that have been submitted with the POST request the script was requested with. But what if there were none, or the script was requested using GET? You got it: it's empty.
In your case, submit doesn't seem to be present in the POST data. In that case you most likely want to just display the form, so the user can enter the data and submit the form (which will set that value). SO you have to check FIRST if "submit" is there.
Second: Your submit button is called "register", and it's value (which is not really necessary, but alright) is "Register". So if the form is submitted, the data that is sent is register=Register&<other fields>. Therefore you have to check for the presence of "register", not "submit"
if (isset($_POST['register']) && $_POST['register']) {
// evaluate
}
you don't have check any var is set so you can see more php error
i change more check, you can try
<html>
<h4>
<center>
New User Registration
</center>
</h4>
<body>
<?php
$errEmail = "";
$errUser = "";
$errPass = "";
//not too sure what this if statement suppose to be.
if(isset($_POST["register"])){//1. no '{' 2. the post is not ckeck 'type', is 'name'
// Email mask
if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\#\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
$errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)#chars(.chars).chars(2-4)</p>';
// User must be digits and letters
if(preg_match("/^[0-9a-zA-Z_]{5,}$/", $_POST["user"]) === 0)
$errUser = '<p class="errText">User must be bigger that 5 chars and contain only digits, letters and underscore</p>';
// Password must be strong
//if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pass"]) === 0)
if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pass"]) === 0)
$errPass = '<p class="errText">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</p>';
}
?>
<form method="post">
<center>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>Username</td>
<td>:</td>
<td ><input name="user" type="text" size="16" value="<?php echo (isset($_POST["user"]))?$_POST["user"]:'';/*3.no check the var is set?*/ ?>">
<?php if(isset($errUser) and $errUser !='') echo $errUser; ?>
</td ><!--4. no '</td>'-->
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="password" size="16" value="<?php echo (isset($_POST["pass"]))?$_POST["pass"]:''; ?>">
<?php if(isset($errPass) and $errPass !='') echo $errPass; ?>
</td >
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" size="50" value="<?php echo (isset($_POST["email"]))?$_POST["email"]:''; ?>">
<?php if(isset($errEmail) and $errEmail !='') echo $errEmail; ?>
</td >
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type='submit' name='register' value='Register'>
</center>
</form>
</body>
</html>
Related
I am new to PHP(loving it already)
I have a form that looks up a table that sends 'golf hole' info back and allows a golfer to input their score of the hole. Problem I have is that I can present the first hole by looking up the hole_detail table but then cant figure out how loop through the table for hole 2, 3.....18 when the form is submitted. I have searched stackoverflow but cant find anything that specific about it. I have tried an if statement, if (isset($_POST['Submit'])) to try increment the $hole_id. Am I completely going about it the wrong way? Thanks in advance.
<?php
include ('../scripts/dbconfig.php');
# get the most recent course name:
$get_course_name = mysql_query("SELECT course_name FROM comp ORDER BY PID DESC LIMIT 1");
$show_course_name = mysql_fetch_array($get_course_name);
if (isset($_POST['Submit'])) {
$hole_id =1;
else {
$hole_id = $hole_id + 1;
}
}
# get the hole yardage and SI from most recent selected golf course:
$get_course_detail = mysql_query("SELECT * FROM `course_detail` WHERE course_name = '". $show_course_name['course_name'] . "'");
$show_course_detail = mysql_fetch_array($get_course_detail);
$get_hole_detail = mysql_query("SELECT * FROM `course_detail`,`phoenix_hole` WHERE Course_ID = 6 AND hole_id = $hole_id");
$show_hole_detail = mysql_fetch_array($get_hole_detail);
?>
</head>
<body>
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><?php echo $show_course_name['course_name'];?></td>
</tr>
<tr>
<td width="20">HOLE <?php echo $show_hole_detail['hole_id']?></td>
<td width="5"> PAR <?php echo $show_hole_detail['hole_par'];?></td>
</tr>
<tr>
<td width="20">Yards</td>
<td width="20">S.I</td>
</tr>
<tr>
<td bgcolor="yellow"><?php echo $show_hole_detail['yellow_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td border="1px" bgcolor="white"><?php echo $show_hole_detail['white_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td bgcolor="red"><?php echo $show_hole_detail['red_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
</table>
</p>
<form id="game_form" name="game_form" method="post" action="game_form.php">
<table width="300" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
<td><b>Hole Shots</b></td>
<td><input name="hole_shots" type="text" class="textfield" id="hole_shots" maxlength="2" size="3" ></td>
<td><b>Putts</b></td>
<td><input name="putts" type="text" class="textfield" id="putts" maxlength="2" size="3"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Next Hole" align="center" /></td>
</tr>
</table>
</form>
</body>
</html>
Or you can use a hidden field that keeps the hole number and you can increment it from php.
$hole_id, in this scenario, will always be 1, because when a user clicks the Submit button, $_POST['Submit'] will always have a value. What you should do instead is have $_POST['Submit'] contain the value of $hole + 1. PHP is not going to "remember" what $hole_id was last time around; it's up to you to remind it. As soon as a request is sent to the browser--unless you're using sessions--PHP forgets everything about that request (HTTP is "stateless").
<?php
if (isset($_POST['Submit'])) {
$hole_id = (int)$_POST['Submit'];
} else {
$hole_id = 1;
}
# other code here
?>
You are on hole #<?php echo $hole_id; ?>.
<form>
<!-- form stuff here -->
<button type="submit" name="Submit" value="<?php echo $hole_id + 1; ?>">Next hole</button>
</form>
Recently I moved my codeigniter website to a new server (goddady). Before this everything was working great with no problems. But now I started to get strange problems with post data, whenever I try to insert data that contains relative paths with dots (../../) and try to submit the form, I get an empty $_POST array. The strange thing is that this happens only with certain forms, not all of them. What could cause such problem?
Here is the form that causes problem:
<?php
if(isset($posts2) && count($posts2) == 1){
$posts2 = $posts2[0];
echo form_open_multipart('professors/update_biography/', array("id" => "professors_edit"));
echo form_hidden('posts2[id]', $posts2->id);
if(isset($user) && count($user) == 1){
$user = $user[0];
echo form_hidden('user[id]', $user->id);
echo form_hidden('user[role]', "Professor");
}
?>
<table class="admin_table">
<tr>
<th>
Биографија
</th>
<td>
<textarea name='posts2[biography]'><?php echo $posts2->biography; ?></textarea>
</td>
</tr>
<tr>
<th>
Биографија EN
</th>
<td>
<textarea name='posts2[biography_en]'><?php echo $posts2->biography_en; ?></textarea>
</td>
</tr>
<tr>
<th>
Cv
</th>
<td>
<p class="old">CV</p>
<input type="file" name='cv' id="pdf"></input>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type='submit' name='submit' value='Зачувај' />
</td>
</tr>
</table>
<?php
echo form_close();
?>
<div class="redButton" style="float:left; width: 150px;">
<?php
if(!isset($prof[0]->id)){ //da ne go prikazuva za profesor
echo anchor('professors/', 'Назад до професори');
}
?>
</div>
<?php
}
?>
got a question, that I can't find a solution for. Before I continue with my question, just a brief overview of what the code is suppose to do (This is for a class project in a beginning php course). User automatically starts on the index.php page. Once the quantities are inputed, it will take them to the login.php page. If they have a login, then it will take them to the invoice.php page. If not, then it will take them to the register.php page, then go to the invoice.php page.
Here's where I run into a problem, how do I match the username and password (inputed from the user registration page) from my user.dat file to the login page? It's frustrating because we really didn't go over it in class :(
Any help will be greatly appreciated, feel free to give feed back on any of the code presented :)
Thank you so much in advance folks! This site has definitely ease some of the coding headaches :P
login.php
<h3>
<center>
Please Login
</center>
</h3>
<?php
include "functions.inc";
//if the form data is clicked... if all valid.. display invoice... otherwise display error
$datafile = "users.dat";
$file = file_get_contents($datafile);
if(!strpos($file, "search string")) {
echo "String not found!";
}
if (array_key_exists('submit', $_POST))
{
if(!strpos($file, "search string")) {
echo "String not found!";
}
header('Location: registration.php');
}
else
if (array_key_exists('register', $_POST))
{
header('Location: invoice.php');
}
?>
<?php//made the user login menu into a nice table that will center the username and password in the middle of the page.?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<form name="form1" method="post" action="invoice.php">
<tr>
<td>Username</td>
<td>:</td>';
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</table>
<center><input type='submit' name='submit' value='Login'></center>
</form>
<?php
/*This code will allow a new user to go to the registration page and register for the site
* before buying anything.
*/
?>
<br>
<form name="form1" method="post" action="registration.php">
<center><input type='submit' name='Register' value='New User? Click here.'></center>
</form>
registration.php
<html>
<h4>
<center>
New User Registration
</center>
</h4>
<body>
<?php
/*
* What this code does, is it takes the users registration information, and stores it in a file.
* After that what I hope to do is to retrive the same file when the user is on the login page and if
* the login matches whats in the file, then proceed to the invoice page.
*/
include "functions.inc";
// Define the datafile to hold the $users array
$datafile = "users.dat";
// See if the user has submitted something
if (array_key_exists('register', $_POST))
{
// Get the new user info
$the_user = $_POST['newUser'];
// Load the file of users and store it in $users
$users = arrayfile_to_array($datafile);
// Validate user name and password
if (user_exists($the_user['ID'], $users))
{
echo "<p><center>Please fill in all text boxes</center></p>";
}
else
{
// If valid, save to the file of users
$users[] = $the_user;
array_to_arrayfile($users, $datafile);
}
}
else
{
if (!file_exists($datafile)) // Data file doesn't exist, so create it
{
$users = array();
array_to_arrayfile($users, $datafile);
}
}
?>
<?php
// my defined error values
$errEmail = "";
$errUser = "";
$errPass = "";
if(isset($_POST["register"])){
// User must be digits and letters
if(preg_match("/^[0-9a-zA-Z]{5,}$/", $_POST['newUser']['ID']) === 0)
$errUser = '<span class="error">Username must be more than 5 characters and contain letters and numbers.</span>';
// Password must be strong
if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST['newUser']['password']) === 0)
$errPass = '<span class="error">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</span>';
//Email validation
if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\#\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST['newUser']['email']) === 0)
$errEmail = '<span class="error">example: chars(.chars)#chars(.chars).chars(2-4)</span>';
}
?>
<form action = "<?= $_SERVER['PHP_SELF'] ?>" method= 'POST'>
<center>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>Username</td>
<td>:</td>
<td ><input name="newUser[ID]" type="text" size="16" value="">
<?php if(isset($errUser) and $errUser !='') echo $errUser; ?>
</td >
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="newUser[password]" type="password" size="16" value="">
<?php if(isset($errPass) and $errPass !='') echo $errPass; ?>
</td >
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="newUser[email]" type="text" size="50" value="">
<?php if(isset($errEmail) and $errEmail !='') echo $errEmail; ?>
</td >
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type='submit' name='register' value='Register'>
</center>
</form>
</body>
</html>
So I Basically copied the following code, execpt renaming columns names.
The form is folowing:
<table align="left" width="30%" border="0" >
<form action="company_edit_php.php" method="post">
<tr><td align="center" bgcolor="#ECE6E6">Location: <td align="center"> <input type="text" value="<?php echo $row['Location']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">User: <td align="center"> <input type="text" value="<?php echo $row['User']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Telephone: <td align="center"> <input type="text" value="<?php echo $row['Telephone']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Email: <td align="center"> <input type="text" value="<?php echo $row['Email']?>"/><br></td></tr>
<td> <input type="submit" value="Change" /></td></tr>
And PHP code:
<?php
$con = mysql_connect("localhost","username","password");
$id = isset($_GET['id']) ? (int)$_GET['id']: 1;
mysql_select_db("Database", $con);
if (isset($_POST['Location'])) {
echo $_POST['Location'];
} else {
echo 'empty';
}
$Location = $_POST['Location'];
Result of this code is Undefined index: Location and from if statement i get "empty".
Why this same code work for another page? What should I do now?
Thank you for your efforts
To correct the index post, use this command at the beginning of the file and uses the variable $Location
$Location = (isset($_POST["Location"])?$_POST["Location"]:"");
You have to change your form because you miss name into your input and to retrieve the value with $_GET you have to use it like this:
<tr><td align="center" bgcolor="#ECE6E6">Location: <td align="center"> <input type="text" name="Location" value="<?php echo $row['Location']?>"/><br></td></tr>
To get the value of textboxes, you need to give them name
<table align="left" width="30%" border="0" >
<form action="company_edit_php.php" method="post">
<tr><td align="center" bgcolor="#ECE6E6">Location: <td align="center"> <input type="text" value="<?php echo $row['Location']?>" name='location'/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">User: <td align="center"> <input type="text" value="<?php echo $row['User']?>" name='user'/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Telephone: <td align="center"> <input type="text" value="<?php echo $row['Telephone']?>" name='phone'/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Email: <td align="center"> <input type="text" value="<?php echo $row['Email']?>" name='mail'/><br></td></tr>
<td> <input type="submit" value="Change" /></td></tr>
Location is not passed in the code that you show. for that reason $_POST['Location'] is undefined. put it inside your if statement to avoid the error.
like:
$Location = 'empty';
if (isset($_POST['Location'])) {
echo $_POST['Location'];
$Location = $_POST['Location'];
} else {
echo 'empty';
}
Complete form
<table align="left" width="30%" border="0" >
<form action="company_edit_php.php" method="post">
<tr><td align="center" bgcolor="#ECE6E6">Location: <td align="center"> <input type="text" value="<?php echo $row['Location']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">User: <td align="center"> <input type="text" value="<?php echo $row['User']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Telephone: <td align="center"> <input type="text" value="<?php echo $row['Telephone']?>"/><br></td></tr>
<tr><td align="center" bgcolor="#ECE6E6">Email: <td align="center"> <input type="text" value="<?php echo $row['Email']?>"/><br></td></tr>
<td> <input type="submit" value="Change" /></td></tr>
Try this:
<?php
$con = mysql_connect("localhost","username","password");
$id = isset($_GET['id']) ? (int)$_GET['id']: 1;
mysql_select_db("Database", $con);
$location= "";
if (isset($_POST['Location'])) {
$location= $_POST['Location']; // <------------- put this here!
//echo $_POST['Location'];
} else {
echo 'empty';
}
echo $location; // <------------- added this
//$Location = $_POST['Location']; // it was erroring here as I am guessing that you were sometimes accessing the page without posting the location. If the variable does not exist, you cannot echo it.. thus the changes above
From what you are writing it is so that either there is no field named "Location" on the page you made, or it is outside of the form (thus not inside ....).
As it works on the site where the original sourcecode has been taken from there are only 2 options there.
The site in question HAS a field named location inside the appropriate form
The site in question suppresses the error message that would come.
In order for your code to work you would need to 1.) put the field "Location" inside the form if it is not already and 2.) You need to "secure" the getting of the variable so that an error is not shown even if Location is empty.
I'm giving different examples for both variants:
1.) Putting Location inside the form
<form action="company_edit.php" method="post">
......
<input type="text" name="Location" id="Location>
......
</form>
2.) Making sure that the error does not occur even if Location does notee xist,....
I would even advice doing the following step for every $_POST and $_GET variable you want
to use in your application as it reduces the chance of the error message that a specific
$_GET/$_POST is not set considerably (although for debugging and finding writing errors
it is good to have the error messages).
if (isset($_POST['Location'])
$Location = $_POST['Location'];
else
$Location='';
On another note as you are giving parameters via POST and you don't put the id into the URL the $_GET of id will always result in an empty id field (thus the "1" is always used as id). For it to work you would either need to use id as post and put an appropriate field into the form or you would have to eidt the "action" part of the form so that id is given as "?id=" parameter there
I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??
<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
<table width="672" border="0">
<?php
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
<td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
<td width="229" bgcolor="#f2f2f2"> <?php echo $info['email']; ?> </td>
<td width="408" bgcolor="#f2f2f2"> <?php echo $info['name']; ?> </td>
</tr>
<?php
$info=mysql_fetch_array($query);
$i++;
}
$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>
</table>
To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.
One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:
<td width="21" bgcolor="#f2f2f2">
<input type="checkbox" name="add"
value="<?php print $info['email']; ?>"/> </td>
If you need the $msg variable to do something, assemble it after the output.
<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>
checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?