I am new to PHP and having trouble with a user form. The code executes and produces a page with a selection box and a submit button. The submit button should prompt a new php file. However, the code in the new PHP file is not being executed. I'm just getting a blank webpage.
<?php
include 'Connection.php';
echo "<form action=\"accountStatusChange.php\" method=\"post\">";
echo "<br/>";
echo "<select name=\"accountStatus\">";
echo "<option value=\"Active\">Active</option>";
echo "<option value=\"Inactive\">Inactive</option>";
echo "</select>";
echo "<input type=\"submit\" name=\"loadAccountStatus\" value=\"Go\"/>";
echo "</form>";
?>
this is file accountStatusChange.php:
<html><body>
<?php
$status = $_POST['accountStatus'];
echo $status;
?>
</body></html>
First of all, make your life easier, try to change your code to:
<?php
include 'Connection.php';
echo '
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>
';
?>
Then in second file:
<html><body>
<?php
if(isset($_POST['accountStatus'])){
$status = $_POST['accountStatus'];
echo $status;
}else{
echo 'form not submitted';
}
?>
</body></html>
Have you uploaded both files to server?
Are both the files in the same directory?
You don't need to use so many echo statements; though there is no problem with your code.
Try with using $_REQUEST; like this:
$status = $_REQUEST['accountStatus'];
<?php
include 'Connection.php';
?>
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>
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 created a html form that is populated by a mysql query. The data has spaces in it.
eg "Bob site", "Sarah home"
When the form is created in the browser I get this:
Bob
site
Sarah
home
Instead of
Bob site
Sarah home.
I know I need to format my data but can't find any examples. My form code is below.
<?php include("includes/header.php"); ?>
<?php
$site = mysql_query("select distinct `site_name` from sept_billing");
?>
<html>
<body>
<h2>Create a new install</h2>
<form method="post" action="actions/newmachine_action.php">
<table border = '0'>
<tr>
<td><b>Site name:</b></td>
<td><input id="site" type="text" name="site" size="40" list="sites" value = 'Choose site name' />
<datalist id="sites">
<?php
while ($row = mysql_fetch_array($site))
{
echo "<option value=".$row[0].">";
}
?>
</datalist></td>
</tr>
</table>
<input type="submit" value="Create" />
</form>
</body>
</html>
<?php include("includes/footer.php"); ?>
Please change this and let me know if this works
echo "<option value=".$row[0].">";
to
echo "<option value='".$row[0]."'>";
This will work
<?php
while ($row = mysql_fetch_array($site))
{ ?>
<option><?php echo $row['name']; ?></option>
<?php } ?>
try
"<option text='".$row[0]."' value='".preg_replace(' ', '-', $row[0])."'></option>"
or
"<option value='".preg_replace(' ', '-', $row[0])."'>".$row[0]."</option>"
i think that value cannot have spaces
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']);
?>
im creating a form which will display data from database, everything is set so far, but i want the form to remember the data in case if user has to write it down again in case of error. I found some similar code concerning only html, but when including php to display a form i find some difficulties for code to remember the last input (current problem is only concerning drop down selection list):
$Type = $_POST['petType']; //this should remember last input
<?php
/*upload form and drop-down selection list*/
echo "
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>\n
<option value='-1'>Type:</option>";
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
echo "<option value='$petType' ";
if($Type == '$petType')
{
echo "? selected='selected'";
// i need to make selected true only for last selected option,
//and redisplay it in the same form again
}
echo ">$petType </option>";
}
echo "</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
?>
try this:
<?php
$Type = $_POST['petType']; //this should remember last input
?>
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' <?php echo $Type == $petType ? "selected=selected":"";?> ><?php echo $petType;?> </option>
}
</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
sidenote: make html and php different , it makes things easier.
You are comparing $Type to a string not a variable.
Try
if($Type == $petType)
I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>