I am trying to use post in a form to save form data from a dropdown as a session in the same file as the form
<?php
session_start();
if(isset($_SESSION['post-data']['surnameid']))
unset($_SESSION['post-data']['surnameid']);
?>
Then in the body of html
<form action="" method="post">
<?php
include 'connect.inc';
$sql = "SELECT surnameid FROM customer";
$result = mysql_query($sql);
echo "<select name='surnameid'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['surnameid'] . "'>" . $row['surnameid'] . </option>";
}
include 'close.inc';
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
$_SESSION['post-data'] = $_POST;
echo $_SESSION['post-data']['surnameid'];
?>
The assiginment to $_SESSION does not work
Try this :
You forgot a double quote when you are putting echoing
make sure that you have the rows in the database ( we can't test that ) but i tested with predefined data and this version works
form.php
<?php
session_start();
?>
<form action="" method="post">
<?php
echo "<select name='surnameid'>";
echo "<option value='1'>test</option>";
echo "<option value='2'>test2</option>";
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if( !empty($_POST)){
$_SESSION['post-data'] = $_POST;
}
if(isset($_SESSION['post-data']['surnameid']))
echo $_SESSION['post-data']['surnameid'];
?>
test_session.php
<?php
session_start();
print_r($_SESSION['post-data']['surnameid']);
?>
Related
Im trying to save a variable in the $_Session[deptselect] and call it in a different page but confused to where to declare it. when i echo $_Session[deptselect] it gives blank.
I have already tried in multiple places like inside the form or below it but nothing seems to work. i want to send it from appointment to appointment2 but appointment2 seems to not get the variable.
I have already tried to use $_post[deptselect] to get the value in second page and i get it there but it disappears as soon as i press submit in the second page.
appointment.php
<?php
include("dbconnection.php");
session_start();
if(!isset($_SESSION['username'])) {
die("Please login");
}
$_SESSION['deptselect']=$_POST['deptselect'];
?>
<form class="" action="appointment2.php" method="post">
<fieldset>
<legend>Appointment</legend>
<label>Select Department</label>
<select class="" name="deptselect" required>
<option ></option>
<?php
$deptsql=mysqli_query($con,"select deptname,deptid from department where status='1'");
while ($row = mysqli_fetch_assoc($deptsql))
{
echo "<option id='deptselect' value='" . $row['deptid'] ."'>" . $row['deptname'] ."</option>";
}
?>
</select>
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
<?php
include("footer.php");
?>
appointment2.php
<?php
session_start();
if(!isset($_SESSION['username']))
{
include('footer.php');
die("Please login");
}
if(!empty($_POST)){
include("dbconnection.php");
$dept=$_SESSION['deptselect'];
echo $dept;
$daate=$_POST['date'];
$doc = $_POST['doc'];
$tiime=$_POST['time'];
$user=$_SESSION['username'];
$pd="SELECT * from patient where name='$user'";
$pid=mysqli_query($con,$pd);
while($row = mysqli_fetch_assoc($pid)){
$piid=$row['pid'];
}
$query="insert into appointment (deptid, docempid,pid,apptime,appdate) VALUES ('$dept','$doc','$piid','$tiime','$daate') " ;
//$res=mysqli_query($con,$query);
// echo "$query";
//echo "$dept";
}
?>
<body>
<form class="" action="appointment2.php" method="post">
<label>Select doctor</label>
<select class="" name="doc" >
<option ></option>
<?php
$dee=$_POST['deptselect'];
$_SESSION['id']=$dee;
$sql="SELECT * FROM doctor where deptid='$dee'";
$docsql= mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($docsql))
{
echo "<option value='" . $row['docempid'] ."'>" . $row['name'] ."</option>";
}
?>
</select>
<br><br><br>
<label>Enter Time</label>
<input type="time" name="time" placeholder="enter time" >
<br><br><br>
<label>Enter date</label>
<input type="date" name="date" placeholder="enter date" >
<br> <br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
<?php include("footer.php");
?>
I want the $_session[deptselect] in appointment2.php so i can run the command the insert, i have zero knowledge of js so doing the dependent list thing is not possible. TIA
In the beginning of the first script you have:
if(!isset($_SESSION['username'])) {
die("Please login");
}
And in the beginning of the second:
if(!isset($_SESSION['username']))
{
include('footer.php');
die("Please login");
}
Both stop immediately, if $_SESSION['username'] is not set.
So how should $_SESSION['username'] ever be set, if your body code is never executed?
I have a form that is being created dynamically
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{ ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<?}?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
</form>
But i a not able to understand how to carry the value of input to the addtable.php page
Can anyone please tell how to submit these values from this form
In your addtable.php, you need to print_r $_POST and see what inputs you're getting.
// addtable.php
echo "<pre>";
print_r($_POST);
print_r($_FILES); // If you're expecting a file input
Also, you should modify your HTML a little:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) { ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<? } ?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button> <!--Place submit button outside loop -->
<?php } ?>
I think there is issue in submit button, you looping submit button with same name, try to put submit button out of while loop.
One of the reasons your PHP is not functioning correctly is because your PHP block, is not placed inside of PHP tags.
Your code should look like this:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php // added the PHP tags
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
?> // escaped the php tag
<input type="text" placeholder="<?= $row['heading']; ?>" name="<?= $row['heading']; ?>" />
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
}
?>
Notice how I also escaped PHP when I wanted to display any HTML. Of course, you could simply echo these out, but you can do this too! Personal preference, really.
Another little alteration I made was, rather than using <?php echo ... ?> you can simply use <?= $row['...']; ?>. It is faster and cleaner too!
In your addtable.php:
<?php
$inputValue = $_POST['input_name'];
//the $inputValue is the input value that you posted from form.
echo $inputValue;
?>
To receive data in addtable.php you have to get them by name property of inputs in the form. So you have to define the names of inputs or to retrieve them again from db in addtable.php
try this ,
addtable.php
<?php
foreach ($_POST as $key => $value){
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
?>
i hope it will be helpful.
You can create array of heading and access it in php:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<input type="text" placeholder="<?php echo $row['heading']; ?>" name="heading[]"> />
<?
}
?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
?>
</form>
In addtable.php:
$headingArray = $_POST['heading']; // here you will get all the heading in array format
So, i have this foreach loop that runs a sql query. Every row is printed in a option. I have two forum posts. One that 'deletes' the row and one that 'uses' the row. But when I post the form the content inside the option remains empty. Here is my code:
Post file
<?php
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$sql = "SELECT * FROM message";
?>
<html>
<form action="action.php" method="post">
<select><?php
foreach ($DB->query($sql) as $row)
{
?>
<option name="content" value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br /><input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Action.php
<?php
require_once 'hidden/session.php';
$delete = $_POST['delete'];
$use = $_POST['use'];
$content = $_POST['content'];
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$delete = "DELETE FROM message WHERE title='$content'";
if (isset($delete)){
$DB->exec($delete);
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
In order to post content you have to give name to your control. You have specified name property for option but there is no name property specified in select. Which is why the value of that control is not getting posted. Hence, when you try to access it as $content = $_POST['content']; , it gives you empty value.
Try this:
<html>
<form action="action.php" method="post">
<select id="content" name="content">
<?php foreach ($DB->query($sql) as $row) { ?>
<option value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br />
<input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Hope it helps!!
I have a droplist <select> list in html. How do I identify the selected value in the $_POST array after the user submits the form ?
<form action="subj_exec.php">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value=';
echo $subject_id;
echo '> ';
echo $name;
echo '</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
</form>
The subject_id is blank in another php file
echo $_POST['subject_id'] is blank.
Please help to identify the issue in the code.
Thanks,
The standard method for forms is GET, so you need to add method="POST" to your form.
get: Default. Appends the form-data to the URL in name/value pairs: URL?name=value&name=value
post: Sends the form-data as an HTTP post transaction
<form action="subj_exec.php" method="POST"> //<<<< added method
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
?>
<option value="<?= $subject_id; ?>"><?= $name; ?></option>
<?php
}
?>
</select>
<input type="submit" value="submit" name="submit" />
In your file subj_exec.php, you can output the selected value with
echo $_POST['subj_id'];
Here you go :
index.php
<form action="subj_exec.php" method="POST">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<select name="subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value="'.$subject_id.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
subj_exec.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit'])) {
if(strlen($_POST['subj_id']) >= 1) {
$option = htmlentities($_POST['subj_id'], ENT_QUOTES, "UTF-8");
// Do Something here with $option
echo $option;
}else {
echo 'nothing selected.';
}
}
?>
It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks