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
Related
I'm trying to return to a page that is dynamically created from data stored in a database. However when I try to return to the main dynamic page it does not work. It should on submit redirect to the page that shares the yard id for the track. Instead it just gives me a failure message
Controller
public function update_track($yard_id)
{
$data = array(
'track_id'=>$this->input->post('track_id'),
'train_id'=>$this->input->post('train_id'),
'train_direction'=>$this->input->post('train_direction'),
'train_length' =>$this->input->post('train_length'),
'train_hpt'=>$this->input->post('train_hpt'),
'train_tons'=>$this->input->post('train_tons'),
'train_status'=>$this->input->post('train_status'),
);
$this->load->model('atisyard_model');
$this->atisyard_model->update_track($data);
if($this->db->affected_rows()>0)
{
redirect('/atis/'.$value->yard_id);
}
else
{
echo 'failure';
}
}
View
<?=form_open('atis/update_track/'); foreach ($record as $value) {?>
<h1>Edit Track:</h1>
<b><p>Track Name: </b><?php echo $value->track_no;?></br>
<b>Track Length: </b><?php echo $value->track_length;?>'</br></br></p>
<table cellpadding="5" border="0" align="center">
<input type="hidden" name="track_id" title="track_id" value="<?php echo $value->track_id;?>">
<tr>
<td>Lead Engine No. / Symbol / Work Order:</td>
<td><input type="text" name="train_id" id="train_id" size="28" maxlength="28" value="<?php echo $value->train_id;?>"></td>
</tr>
<tr>
<td>Train Direction:</td>
<td><?=form_dropdown('train_direction', $train_direction, $value->train_direction);?></td>
</tr>
<tr><td>Train Length:</td>
<td><input type="text" name="train_length" id="train_length" size="4" maxlength="6" value="<?php echo $value->train_length;?>"></td>
</tr>
<tr><td>HP/T:</td>
<td><input type="text" name="train_hpt" id="train_hpt" size="2" maxlength="5" value="<?php echo $value->train_hpt;?>"></td>
</tr>
<tr><td>Train Tons:</td>
<td><input type="text" name="train_tons" id="train_tons" size="4" maxlength="6" value="<?php echo $value->train_tons;?>"></td>
</tr>
<td>Train Status:</td>
<td> <?=form_dropdown('train_status', $train_status, $value->train_status);?></td>
</tr>
<tr>
<tr><td><button id="submitbtn" >Submit</button></td></tr>
</table>
Back to Yard
<?php } echo form_close();?>
Taking a second look at your code, would it be better to add the yard_id as a hidden field on your form:
<input type="hidden" name="yard_id" name ="<?=$value->yard_id; ?>" />
then change your redirect to:
redirect('/atis/'.$this->input->post('yard_id'));
If I understand this correctly and you are passing $yard_id into your update_track() function, then shouldn't this:
redirect('/atis/'.$value->yard_id);
Be just :
redirect('/atis/'.$yard_id);
Because I can't see where your getting $value from.
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>
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>
I've got a simple form that I would like to be able to add a hidden value that is only used in the event the user doesn't enter a value. The values are for simple text input fields and can use HTML, PHP, JavaScript, JQuery, or any combination thereof. Let me explain further that the value attributes are already set to contain the values in the DB for that form currently. However, in the event there is no current value in the DB for that specific field and the user does not enter a value themselves, I would like a default value to be passed in the POST.
I've done some research and found examples of custom attributes, etc... but these will not work for me. As I'm using an array builder that passes all of the fields in the form as a single array to a new function. Any help would be greatly appreciated!
The code shown below is the creation of the form... As you can see two separate fields are created redundantly for however many rounds exist within the tournament. I've also provided output HTML code of a two round form. In the event the DB has NULL values for the field in question, and if the user doesn't enter a value of his/her own, I would like to pass the value of "false".
$rchkpos = array();
$rchkscore = array();
for ($i=0; $i<=$tournament['numRounds']; $i++)
{
$rchkthis = array("$i" => "round".$i."pos");
$rchkthis2 = array("$i" => "round".$i."score");
$rchkpos = array_merge($rchkpos, $rchkthis);
$rchkscore = array_merge($rchkscore, $rchkthis2);
if ($i > 0)
{
$roundpos = $rchkpos[$i];
$roundscore = $rchkscore[$i];
if (!is_null($stats2[$roundpos])){
$roundposvar = "$stats2[$roundpos]";
}else{
$roundposvar = false;
}
if (!is_null($stats2[$roundscore])){
$roundscorevar = "$stats2[$roundscore]";
}else{
$roundscorevar = false;
}
$team_stats.="
<tr valign='top'>
<td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_POSITION . "</td>
<td class='alt1' align='center'>
<input type='text' name='round[".$rchkpos[$i]."]' value='$stats2[$roundpos]' size='40' maxlength='5' />
</td>
</tr>
<tr valign='top'>
<td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_SCORE . "</td>
<td class='alt1' align='center'>
<input type='text' name='round[".$rchkscore[$i]."]' value='$stats2[$roundscore]' size='40' maxlength='5' />
</td>
</tr>";
}
}
Output HTML Example:
<tbody><tr valign="top">
<td align="center">Round 1 Position</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="2" name="round[round1pos]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 1 Score</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round1score]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 2 Position</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round2pos]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 2 Score</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round2score]">
</td>
</tr>
</tbody>
You can possibly do it like this:
HTML
<input type="text" name="name" />
<input type="hidden" name="name_default" value="name_default" />
PHP (after form submit)
if(!isset($_POST['name'])) {
$_POST['name'] = $_POST['name_default'];
}
Or make an aray like this:
$array_check = array('name', 'telephone', 'gender');
foreach($array_check AS $key => $value) {
if(!isset($_POST[$value])) {
$_POST[$value] = $_POST[$value."_default"];
}
}
Keep in mind that users can edit the values of the hidden inputs (with developer tools like Firebug), always check the input!
Other possible approach is to set the default values in a dedicated property in each html input and then replace the value of the field before the post using jQuery:
// before post
$('input').each(function () {
if ($(this).val() == '')
$(this).val($(this).attr('defaultvalue'));
});
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?