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
}
?>
Related
I have just written this code in which I have a form. You have to write your name, surname and country. You also have to choose your favourite colour. After that, you push a submit button so that you can see the data afterwards. I'm using the GET method with 1 page, but I have to use a second one with the POST method so that each echo is on that second page.
How could I do that? My code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
<link rel="stylesheet" type="text/css" href="form.css">
</head>
<body>
<section>
<?php
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
<h1>Form</h1>
<form class="elegant" method="GET" action="?">
<fieldset>
<legend>Favourite colour</legend>
<div>
<label for="nombre">Name</label>
<input type="text" placeholder="Name" name="name"
id="name" />
</div>
<div>
<label for="surname">Surname</label>
<input type="text" placeholder="Surname" name="surname"
id="surname" size="50" />
</div>
<div>
<label for="country">Country</label>
<input type="text" placeholder="Country" name="country" id="country"
size="10" maxlength="9" />
</div>
<div>
<select name="colour" id="colour">
<option value="yellow" <?php if ($colour == "yellow" ) echo "selected" ?> >yellow</option>
<option value="red" <?php if ($colour == "red" ) echo "selected" ?> >red</option>
</div>
<input class="btn" type="submit" value="Save" />
</fieldset>
</form>
<?php
}
?>
</section>
</body>
</html>
I know i have to use a link to that second page, but that's all I know. Thanks in advance!
If I understand correctly, the code you show is for Page 1 where the user can:
Input data if data don't exist;
View data and confirm them if they exist. At this point store data in SESSION and send the user to an other page.
To do so, remember you have to add the session_start() command right at the beginning of every page in which you will be able to manipulate Session Data.
<?php
session_start();
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
$_SESSION["name"] = $name;
$_SESSION["surname"] = $surname;
$_SESSION["country"] = $country;
$_SESSION["colour"] = $colour;
Confirm
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
...
In "another_page.php" then you will find that you can access your data simply querying the $_SESSION array.
<?php
session_start();
...
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br>";
echo "Surname is " . $_SESSION["surname"] . ".";
// etc.
?>
Complete reference is the PHP Manual and simple reference is in W3C PHP Sessions page.
In the form tag you can specify where to form gets submitted to:
<form class="elegant" method="GET" action="YOUR_PAGE_URL_HERE">
On that second page you get the values via the $_GET Variable like
<?php
echo $_GET['name'].' '.$_GET['surname'];
I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.
This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if
Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here
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.
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
echo "<form method=\"post\" action=mainpage.php?subject_id=".$current_subject."¬e_id=".$current_content.">";
?>
<input type='text' name='list_item' value=''>
<input type='submit' name="new_item" value="New Item">
</form>
The problem is that when one of the GET variables is two words the link doesn't write it that way. So for example if $current_subject="Advanced Chemistry" and $current_content="Valence Electrons" the link will come out as:
<form method="post" action="mainpage.php?subject_id=Advanced" chemistry¬e_id="Valence" electrons>
You need to urlencode() the variables like so:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
$subject = urlencode($current_subject);
$content = urlencode($current_content);
$action = "mainpage.php?subject_id=" . $subject . "¬e_id=" . $content;
?>
<form method="post" action="<?php echo $action; ?>">
<input type="text" name="list_item" value="">
<input type="submit" name="new_item" value="New Item">
</form>
Also, you should get in the habit of validating that data. You probably want to check that they are integers.
Use urlencode() or rawurlencode()
Always quote your attributes and escape your data. Quoted, it would work:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
echo "<form method=\"post\" action=\"mainpage.php?subject_id=" . $current_subject . "¬e_id=" . $current_content . "\">";
?>
<input type="text" name="list_item" value="" />
<input type="submit" name="new_item" value="New Item" />
</form>
But, of course, you should urlencode it first:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
$url = 'mainpage.php?subject_id=' . urlencode($current_subject) . '¬e_id=' . urlencode($current_content);
?>
<form method="POST" action="<?php echo $url; ?>">
<input type="text" name="list_item" value="" />
<input type="submit" name="new_item" value="New Item" />
</form>
I would probably use http_build_query:
$query = http_build_query(array('subject_id' => $_GET['subject_id'], 'foo' => 'bar'));
<form action="mainpage.php?<?php echo $query; ?>">
I have a suspicion that $query should also be htmlentities'd.
The http_build_query handles the URI encoding, but I'm not sure if it should also be HTML-encoded on top of that (it is an HTML attribute after all).
You should have a look at PHPs urlencode()
$current_subject = urlencode($_GET['subject_id']);
$current_content = urlencode($_GET['note_id']);
I need to insert some form values into a db table , how would I create a function to call once the user clicks on the button and run the insert scrtipt .
<form name="quiz_info" method="post">
<?php
echo $this->quiz->title;
echo $mainframe->getPageTitle();
echo '<p><input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">' . JText::_('I have Read and Acknowledge the procedure'). '</label></p>' ;
echo '<input id="proceedButton" name="proceedButton" disabled="true" value="' . JText::_('Acknowledge') . '" type="submit" />' ;
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge` (course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
?>
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP is executed on the server-side. If you want to call the function without reloading the page you will have to use an AJAX call, for example with jQuery.
You can find billions of tutorials through google.
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge`(course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $this->quiz->title; ?>
<?php echo $mainframe->getPageTitle(); ?>
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed"><?php echo JText::_('I have Read and Acknowledge the procedure'); ?></label>
<input id="proceedButton" name="proceedButton" disabled="true" value="<?php JText::_('Acknowledge'); ?>" type="submit" />
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP doesn't work this way. If you wish to activate your PHP script on form submit, you'll need to submit the form to a PHP page, and on that page to put your script, for example
<form action=submit.php method=post>
<input type=text name=text>
<input type=submit>
</form>
Next, on submit.php:
<?php
if (!empty($_POST['text'])) { //If the POST variable set by input named 'text' is not empty...
echo $_POST['text']; //Print it on the screen.
} else { //If it is empty
echo "No form submission detected"; //Print an error
}
?>
If you want it to work without a page reload, you'll have to use some client side technology. The most popular one for that purpose is AJAX