How would I go about doing this?
<?php
if (isset ($_SESSION['ID'])) {
echo "
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '" . echo $_SESSION['ID'] . "' placeholder = 'Email' size = '30' required/>
</form>
?>
I'm trying to pull a var from the session and put it inside a form value and can't figure out how to do so.
It's not recommended to echo your whole html in PHP... You could do it like this:
<?php if(isset($_SESSION['ID'])): ?>
<form action='updateacct.php' method='POST'>
Email: <input type='text' name='eml' value='<?php echo $_SESSION['id']; ?>' placeholder='Email' size='30' required/>
</form>
<?php endif; ?>
No need for the second echo. You are already echoing.
I took your code and simplified it a bit. I use multiple echos to make it clearer what we do.
<?php
if (isset($_SESSION['ID'])) {
echo '<form action="updateacct.php" method="POST">';
echo ' Email:';
echo ' <input type="text" name="eml" value="' . $_SESSION['ID'] . '" placeholder="Email" size="30" required />';
echo '</form>';
}
?>
I would go like this:
<?php if (isset ($_SESSION['ID'])) : ?>
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '<?= $_SESSION['ID'] ?>' placeholder = 'Email' size = '30' required/>
</form>
<?php endif; ?>
You can say:
<?php
if (isset ($_SESSION['ID'])) {
?>
// HTML goes here
<?php
}
?>
I seem to be having trouble retrieving some values. For this PHP program, I have to store my first name, last name, phone number, a value for a radio button list, and a selecting a computer game from a select box/drop down list. What I have to do is store these values from the Ses1 file and retrieve them from the Ses2 file. At the moment, I can store all the 5 different values. When I hit the "Submit Information" button I can only retrieve the first name, last name, and telepehone number values. What can I do to retrieve the value from the radio button list and computer game from the drop down list. My current code down below.
<?php
// we always have to start session state
session_start();
if(isset($_POST["firstNameTextBox"]))
{
$_SESSION["firstName"] = $_POST["firstNameTextBox"];
header("Location: Session2.php");
}
if(isset($_POST["lastNameTextBox"]))
{
$_SESSION["lastName"] = $_POST["lastNameTextBox"];
header("Location: Session2.php");
}
if(isset($_POST["telephoneNumberTextBox"]))
{
$_SESSION["telephoneNumber"] = $_POST["telephoneNumberTextBox"];
header("Location: Session2.php");
}
if (isset($_POST["occupation"]))
{
$_SESSION["staff"] = $_POST["occupation"];
$_SESSION["sudent"] = $_POST["occupation"];
$_SESSION["faculty"] = $_POST["occupation"];
header("Location: Session2.php");
}
if(isset($_POST["games"]))
{
$_SESSION["League of Legends"] = $_POST["games"];
$_SESSION["Fallout 4"] = $_POST["games"];
$_SESSION["Overwatch"] = $_POST["games"];
$_SESSION["DOTA 2"] = $_POST["games"];
header("Location: Session2.php");
}
?>
<html>
<head>
<title>Ses1</title>
</head>
</head>
<body>
<form method="post">
<label for = "firstNameTextBox">Enter your first name:</label>
<input type="text" name="firstNameTextBox" value="Put your first name here" />
<br />
<label for = "lastNameTextBox">Enter your last name:</label>
<input type="text" name="lastNameTextBox" value="Put your last name here" />
<br />
<label for = "telephoneNumberTextBox">Enter your telephone number:</label>
<input type="text" name="telephoneNumberTextBox" value="Put your telephone number here" />
<br />
<input type="radio" name="occupation" value = "staff" />
<?PHP print $staff; ?>
Staff
<input type="radio" name="occupation" value= "student" />
<?PHP print $student; ?>
Student
<Input type="radio" name="occupation" value= "faculty" />
<?PHP print $faculty; ?>
Faculty
<br />
<select name="games">
<option value="">Select one computer game...</option>
<option value="League of Legends">League of Legends</option>
<option value="Fallout 4">Fallout 4</option>
<option value="Overwatch">Overwatch</option>
<option value="DOTA 2">DOTA 2</option>
</select>
<br />
<input type="submit" value="Submit Information" />
</form>
</body>
<html>
<head>
<title>Ses2</title>
</head>
<body>
<?php
// again, make sure the session is available for use
session_start();
if(isset($_SESSION["firstName"]))
{
echo "Your first name is " . $_SESSION["firstName"];
}
echo "<br />";
if(isset($_SESSION["lastName"]))
{
echo "Your last name is " . $_SESSION["lastName"];
}
echo "<br />";
if (isset($_SESSION["telephoneNumber"]))
{
echo "Your telephone number is " . $_SESSION["telephoneNumber"];
}
echo "<br />";
if (isset($_SESSION["occupation"]))
{
echo "Your occupation is " . $_SESSION["occupation"];
}
echo "<br />";
if(isset($_SESSION["games"]))
{
echo "The computer game you've chosen was " . $_SESSION["games"];
}
?>
</body>
You need not store each radio button/select box options within the SESSION array.
Your SESSION array index should match the index of the POST array.
E.g: $_SESSION["occupation"] = $_POST["occupation"] instead of
`$_SESSION["staff"] = $_POST["occupation"];
$_SESSION["sudent"] = $_POST["occupation"];
$_SESSION["faculty"] = $_POST["occupation"];`
Try this:
if (isset($_POST["occupation"])) {
$_SESSION["occupation"] = $_POST["occupation"];
}
if (isset($_POST["games"])) {
$_SESSION["games"] = $_POST["games"];
header("Location: Session2.php");
}
I'm having a problem with my sessions forms. It's probably a simple answer but I can't seem to figure it out.
My html:
<body>
<h1>Hobby Selection Page<br /></h1>
<form method="post" action="page1.php" id="hobbies" >
<p>
<label for="Name">Name: </label>
<input type="text" id="name" name="name"/>
</p>
<p>What is your favorite thing to do?<br/>
<select name="hobby">
<option value="movies">Movies</option>
<option value="read">Reading</option>
<option value="music">Music</option>
<option value="other">My hobby is not listed here</option>
</select>
<br/><br/>
<input type="submit" value="Submit Form"/> <input type="reset" value="Clear Form"
/>
</p>
</form>
</body>
First php page (page1.php):
<?php
session_start();
echo 'Click the link below';
$_SESSION['name'] = " . name . ";
$_SESSION['hobby'] = " . hobby . ";
// Second page
echo '<br />page 2';
?>
Second php page (page2.php):
<?php
// page2.php
session_start();
echo 'Your favorite activity:<br />';
echo $_SESSION['name'];
echo $_SESSION['hobby'];
echo '<br />page 1';
?>
My aim is to input data from the first html page and then send it to the first php page (to recieve the session variables) and then to be directed to the second php page where it should retrieve the session variables and display the input data I have entered/selected. I am able to get the html and php pages to display and be linked from one page to the next but I am unable to display (in first php page) the data I have inputted from the html page.
your first page1.php should be like this.
<?php
session_start();
echo 'Click the link below';
$_SESSION['name'] = $_POST['name'];
$_SESSION['hobby'] = $_POST['hobby'];;
// Second page
echo '<br />page 2';
?>
You are not getting data because you are storing string in $_SESSION not the text you entered in fields.
In page1.php
Replace
$_SESSION['name'] = " . name . ";
$_SESSION['hobby'] = " . hobby . ";
By
$_SESSION['name'] = $_POST['name'];
$_SESSION['hobby'] = $_POST['hobby'];
You're missing a few things. First give your submit button a name, like this:
<input type="submit" value="Submit Form" name="my_submit" />
Then, on page1.php, change this:
$_SESSION['name'] = " . name . ";
$_SESSION['hobby'] = " . hobby . ";
To something like this:
// Check if form was submitted. If so, get inputs.
if (isset($_POST['my_submit'])) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['hobby'] = $_POST['hobby'];
}
That should get you started in the right direction. Later, you can add some validation to ensure that $_POST['name'] and $_POST['hobby'] are filled in correctly before assigning them to the $_SESSION
Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>
Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.
I found this site providing code for creating, reading, updating and deleting. I am confused about how to add checkboxes, radio buttons, and dropdowns
http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/
I'm not concerned with the pagination at all——my primary concern is to be able to put in two dropdowns, yes/no radio button, and a collection of 3 checkboxes using PHP.
My attempts were useless as when I tried to edit choices the values did not stay. Included are my three files: pets, editpets, and view.(I changed the file name for database, etc.)
<?php
function renderForm($first, $last, $pets, $size, $type, $years, $error)
{
?>
<!DOCTYPE html>
<html>
<head>
<title>New Customer</title>
</head>
<body>
<?php
// display possible errors
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>First Name:</strong> <input type="text" name="firstname" value="<?php echo
$first; ?>" />
<strong>Last Name:</strong> <input align="center" type="text" name="lastname" value="<
?php echo $last; ?>" /><br/>
<p><strong>No. Pets </strong>
<select name="pets">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</p>
<br/>
<strong>Size? </strong>
<br/>
Big<input type="radio" value="Yes" name="size" checked><?php echo $size; ?><br />
Small<input type="radio" value="No" name="size"<?php echo $size; ?><br />
<br />
<p><strong>Type</strong><br/>
<input name="type[]" type="checkbox" id="type[]"/>
Cats
<input name="type[]" type="checkbox" id="type[]"/>
Dogs
<input name="type[]" type="checkbox" id="type[]"/>
Others
</p>
<br/>
<strong>Years? </strong>
<select name="year">
<option value="Five or Less">Five or More</option>
<option value="Six or More">Six or More</option>
</select><br/>
<input style="color:purple;" type="submit" name="submit" value="Create My Order :-)">
</div>
</form>
<center>Click Here for Orders</center>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if my form submits and, upon triumph, process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, check its validity
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$pets = $_POST['pets'];
$size = mysql_real_escape_string(htmlspecialchars($_POST['size']));
$type = serialize(mysql_real_escape_string(implode(',', $_POST['type'])));
$years = mysql_real_escape_string(htmlspecialchars($_POST['years']));
// check to make sure everything is filled!
if ($firstname == '' || $lastname == '' || $pets == '' || $size == '' || $type == '' || $years == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, show the form again
renderForm($firstname, $lastname, $pets, $size, $type, $years, $error);
}
else
{
// save the data to the database
mysql_query("INSERT customers SET firstname='$firstname', lastname='$lastname', pets='$pets', size='$size', type='$type', years='$years'")
or die(mysql_error());
// once saved, redirect to cheview page
header("Location: view.php");
}
}
else
// if the form is not submitted, show my form again.
{
renderForm('','','','','','','');
}
?>
<?php
$types = array("Cats", "Dogs", "Others");
function renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>First Name: </strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br/>
<strong>Last Name: </strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br/>
<p><strong>No. Pets </strong>
<select name="pets">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</p>
<strong>Size? </strong>
<br/>
Big<input type="radio" value="Yes" name="size" checked><?php echo $size; ?><br />
Small<input type="radio" value="No" name="size"<?php echo $size; ?><br />
<br />
<br />
<br />
<p><strong>Type</strong><br/>
<input name="type[]" type="checkbox" id="type[]"/>
Cats
<input name="type[]" type="checkbox" id="type[]"/>
Dogs
<input name="type[]" type="checkbox" id="type[]"/>
Others
</p>
<br/>
<strong>Years? </strong>
<select name="year">
<option value="Five or Less">Five or More</option>
<option value="Six or More">Six or More</option>
</select><br/>
<br /><br />
<input type="submit" name="submit" value="Resubmit My Order :-)">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// check for id being an integer
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$pets = $_POST['pets'];
$size = mysql_real_escape_string(htmlspecialchars($_POST['size']));
$type = serialize(mysql_real_escape_string(implode(',', $_POST['type'])));
$years = mysql_real_escape_string(htmlspecialchars($_POST['years']));
// check that firstname/lastname fields are both filled in
if ($firstname == '' || $lastname == '' || $pets == '' || $size == '' || $type == '' || $years == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE customers SET firstname='$firstname', lastname='$lastname', pets='$pets', size='$size', type='$type', years='$years' WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM customers WHERE id='$id' ")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$pets = $row['pets'];
$size = $row['size'];
$type = serialize($row['type']);
$years = $row['years'];
// show form
renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View customer orders</title>
</head>
<body>
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM customers")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Pets</th>
<th>Size</th>
<th>Type</th>
<th>Years</th>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['pets'] . '</td>';
echo '<td>' . $row['size'] . '</td>';
echo '<td>' . $row['type']. '</td>';
echo '<td>' . $row['years'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
I'm honestly lost at this point. I've been working on this for an entire month and I have no idea what I'm doing any more. I'd be so thankful if anyone can help me to edit this to work.
Thanks everyone.
Right, to get your saved results from the database I'm sure you're aware of how to do this.
For your radio buttons you're going to want to select your 'size' value from the database and use an if statement to determine which radio button will be 'checked'
forename/surname is a matter of simply getting the forename/surname from database and setting the appropriate input tags to the acquired values
You've already posted a technique for handling the combo/drop-down boxes
you'd perform a similar process for the 'type' checkboxes, compare your database values to the values of your checkbox input tags and if the value matches, set the checkbox to checked.
Next time please be more specific in what you are asking for help with and separate your code into segments that correspond with the files it is contained in.
if you have 3 files, have a code block for each file.