It was working until I tried adding this statement
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
I was able to get the indexnum from my form before but when I added that line nothing seems to load in the fields.
Here is the full form:
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
Its getting late and I probably need to just go to sleep but IM SO CLOSE!!! Here is the full code that processes the POST.
if($user_data['permissions'] >= 1)
{
// If users permission is 1 or 2 they get a field for inputting the index # and a button to change the approve field from 0 to 1 may need to make a new field to record who approved it....
//Determine if the order is already approved. If not approved show index field and allow user to approve it with index number
if($data2[0]['Approved'] == 1)
{
echo " <font color=\"green\"> Approved";
}
if($data2[0]['Approved'] == 0)
{
echo " Not Approved. Supply an index number and click approve to authorize this order to be completed.";
if (empty ($_POST) === false)
{
$required_fields = array('IndexNum');
foreach ($_POST as $key=>$value)
{
if (empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if (isset($_POST['success']) === true && empty($_POST['success']) === true)
{
echo 'Index has been updated and Order is now set to Approved';
}
else
{
if (empty($errors) === true)
{
$indexnum=$_POST['IndexNum'];
$approvedby=$user_data['lname'];
$vendorid1= $_POST['hidden1'];
echo $indexnum;
echo $approvedby;
echo $vendorid1;
//update_approved($indexnum, $approvedby, $vendorid1);
//header('Location: index.php');
//exit();
}
else if(empty($errors) === false)
{
echo output_errors($errors);
}
}
}
?>
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
<?php }
}
Thank you all for looking into this. I get that $id value from a previous POST. I use it elsewhere in the code without issue.
Thank you all so much!
Try like
<input type="hidden" name="hidden1" value="<?php echo $id;?>">
Also try like
<?php echo '<input type="hidden" name="hidden1" value=".$id.">' ?>
Related
I'm supposed to make 1 PHP file called "access.php" that contains 11 pages. I have to do this with 1 PHP file. I'm supposed to accomplish this using functions to display each page. The "welcome" page allows a user to select "Administrator" or "User" via radio button. After hitting submit, the corresponding page should appear. Right now the admin/user page functions simply echo "Administrator Page" or "User Page" for testing purposes.
Ideally, a new page should appear that displays one of those centered at the top of the screen. But what's happening right now is the text "Administrator Page/User Page" just appears at the bottom of the welcome screen.
Here is my code:
<?php
#---Functions---
#**PHP Header**
#function phpHeader(){
#print <<< EOF
#<!DOCTYPE html>
#<head>
#</head>
# <body>
#EOF
#}
#**PHP Footer**
#function phpFooter() {
#echo "</body> </html>";
#}
#**Admin Page**
function adminPage(){
#phpHeader();
echo "<center><h1>Administrator Page</h1></center>";
#phpFooter();
}
#**User Page**
function userPage(){
#phpHeader();
echo "<center><h1>User Page</h1></center>";
#phpFooter();
}
#**Welcome Page**
function welcomePage(){
#phpHeader();
echo "<center><h1>Welcome</h1>";
echo "<br><br><br>";
echo "<h3> Select Login Type</h3>";
echo "<br><br><br>";
echo '<form name="access" action="" method="post">';
echo '<input type="radio" name="AccessLevel" value="admin" />Administrator <br />';
echo '<input type="radio" name="AccessLevel" value="user" /> User <br /><br />';
echo '<input type="submit" name="submit" value="Choose Level" />';
echo '</form></center>';
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
adminPage();
}
else {
userPage();
}
}
#phpFooter();
}
welcomePage();
?>
Move this entire block at the top of your page, right under your opening <?php tag.
Sidenote: You can use return; or exit; or die();, the choice is yours; I used return;
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
echo adminPage();
return;
}
else {
userPage();
return;
}
}
Which will echo only "Administrator Page" or "User Page" at the top of the page and nothing else.
<?php
function adminPage(){
return "<center><h1>Administrator Page</h1></center>";
}
function userPage(){
return "<center><h1>User Page</h1></center>";
}
function welcomePage(){
return '<center><h1>Welcome</h1>
<br><br><br>
<h3> Select Login Type</h3>
<br><br><br>
<form name="access" action="" method="post">
<input type="radio" name="AccessLevel" value="admin" />Administrator <br />
<input type="radio" name="AccessLevel" value="user" /> User <br /><br />
<input type="submit" name="submit" value="Choose Level" />
</form></center>';
}
$AccessLevel = $_POST['AccessLevel'];
if(isset($_POST['submit'])){
if(!isset($AccessLevel)){
echo welcomePage();
}elseif($AccessLevel=="admin"){
echo adminPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}elseif($AccessLevel=="user"){
echo userPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}
}else{//no form submitted
echo welcomePage();
}
?>
I am using checkboxes to query the database and I am struggling with this one, I am new to MySQL and PHP so sorry if this is simple!
Here is my code that I have...
<input type="checkbox" name="season2005" value="2005" <?php if(isset($_POST['season2005'])) echo "checked='checked'"; ?> > 2005-06
<input type="checkbox" name="season2006" value="2006" <?php if(isset($_POST['season2006'])) echo "checked='checked'"; ?> > 2006-07
<input type="checkbox" name="season2007" value="2007" <?php if(isset($_POST['season2007'])) echo "checked='checked'"; ?> > 2007-08
<input type="checkbox" name="season2008" value="2008" <?php if(isset($_POST['season2008'])) echo "checked='checked'"; ?> > 2008-09
<input type="checkbox" name="season2009" value="2009" <?php if(isset($_POST['season2009'])) echo "checked='checked'"; ?> > 2009-10
<input type="checkbox" name="season2010" value="2010" <?php if(isset($_POST['season2010'])) echo "checked='checked'"; ?> > 2010-11
<input type="checkbox" name="season2011" value="2011" <?php if(isset($_POST['season2011'])) echo "checked='checked'"; ?> > 2011-12
<input type="checkbox" name="season2012" value="2012" <?php if(isset($_POST['season2012'])) echo "checked='checked'"; ?> > 2012-13
<input type="checkbox" name="season2013" value="2013" <?php if(isset($_POST['season2013'])) echo "checked='checked'"; ?> > 2013-14
if (#$_POST['season2005'] == ""){ $season2005 = "0000"; } else { $season2005 = "2005"; }
if (#$_POST['season2006'] == ""){ $season2006 = "0000"; } else { $season2006 = "2006"; }
if (#$_POST['season2007'] == ""){ $season2007 = "0000"; } else { $season2007 = "2007"; }
if (#$_POST['season2008'] == ""){ $season2008 = "0000"; } else { $season2008 = "2008"; }
if (#$_POST['season2009'] == ""){ $season2009 = "0000"; } else { $season2009 = "2009"; }
if (#$_POST['season2010'] == ""){ $season2010 = "0000"; } else { $season2010 = "2010"; }
if (#$_POST['season2011'] == ""){ $season2011 = "0000"; } else { $season2011 = "2011"; }
if (#$_POST['season2012'] == ""){ $season2012 = "0000"; } else { $season2012 = "2012"; }
if (#$_POST['season2013'] == ""){ $season2013 = "0000"; } else { $season2013 = "2013"; }
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
$seasonpick = implode(",",$seasons);;
$matcharrays = array("AND season in ($seasonpick)");
At the moment all of the data is being queried to the database, so if nothing is selected them then part of query from this is "AND season in (0000,0000,0000,0000) etc
How would I go about only getting those selected into the array and if none are selected then the array would be blank.
Hope you understand what I mean!
Here is a working form with some checkboxes that will allow you to test and get the sql you intended.
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
echo "<input type=\"checkbox\" name=\"season[]\" value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
Output when 2009, 2010 and 2011 selected:
.... and season in (2009,2010,2011)
Okay, so how it works:
Checkboxes are best used when they all have the same name ending in a []. This makes it a nice array on it's own.
If post data is set, we then quickly throw an array unique over it (good habit for the most part in these types of queries) so that there are no duplicate values.
Then simply implode it into a string and pop it into the SQL query.
Edit: Added functionality to re-check checkboxes when submitted.
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array($i+2005, $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
Edit 2: Just add quotes in the right places :)
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode("', '", $dateArr);
$sql=".... and season in ('".$dateSearch."')";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array(($i+2005)."i", $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".(($i+2005)."i")."\"> ".($i+2005)."i";
}
?>
<input type="submit">
</form>
Edit 3: I feel like this is starting to really answer much more than one question :)
You can simply check the textbox to make sure it isn't empty and then append to a SQL string:
$sql="";
if(!empty($_POST['text1']))
{
$sql.=" and ftgf>= ".$_POST['text1']." ";
}
Having said that, I would strongly suggest that you NEVER allow the user to enter in parts of the actual SQL you will run - unless it is a closed/secure environment, which means NOT an ope website.
Insert the below code
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
//start
$seasons2 = array();
foreach ($seasons as $season)
{
if($season!=="0000")
{
array_push($seasons2,$season);
}
}
$seasonpick = implode(",",$seasons2);
//end
Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}
i need to solve this issue i have two different session variables and i want echo message and radio button name size values in if and else condition via session variable?
Problem is that when i select radio button it should be display size values instead of accessories?
For Example I Want Like This
If Input Values Like Any Number
echo "Accessories"; // It Should Be Display Accessories If
Any Kind Of Numbers Enter.
Else If Radio Select
echo size name // display size whatever choose
from radio button.
Here's my code:
<?php
session_start();
if(isset($_REQUEST['test']))
{
$_SESSION['size1']=$_POST['size'];
$new_sizes=$_SESSION['size1'];
$_SESSION['cid1']=$_POST['cid'];
if(isset($_SESSION['cid1']))
{
echo "Accessories";
}
else
{
echo $new_sizes;
}
}
?>
<form method="post">
<input type="text" name="cid">
<br>
<label for="Small">Small</label>
<input type="radio" name="size" id="Small" value="Small" />
<label for="Medium" >Medium</label>
<input type="radio" name="size" id="Medium" value="Medium"/>
<label for="Large">Large</label>
<input type="radio" name="size" id="Large" value="Large"/>
<label for="Xl">Xl</label>
<input type="radio" name="size" id="Xl" value="Xl"/>
<br>
<input type="submit" name="test" value"Submit">
</form>
Maybe you should try
something like
if(is_numeric($_SESSION['cid1']))
{
echo "Accessories";
}
else
{
echo $new_sizes;
}
Re-Write code like this:
<?php
session_start();
session_destroy();
session_start();
if (empty($_POST) === false) {
if (empty($_POST['size']) === false) {
$_SESSION['size1']=$_POST['size'];
echo $new_sizes = $_POST['size'];
} else if (empty($_POST['cid']) === false) {
$_SESSION['cid1']=$_POST['cid'];
echo "Accessories";
}
}
?>
I've been at this the whole day. First I tried to validate my form in php but finally gave up. Now I'm trying to validate the form with JavaScript. It works but since I don't know JavaScript at all and have limited knowledge of PHP I really need help please. It is a test and I want to validate that each question has been answered. The field name in the form = php variable/array, can't get this to work in the javascirpt
var x=document.forms["myForm"]["question_{$q_nr}"].value;
. If I just use a text field name it works fine.
The form
<html>
<head>
<?php
session_start();
?>
<script type="text/javascript" src="counter.js"></script>
<script language="JavaScript">
<!--
function validateForm()
{
var x=document.forms["myForm"]["question_{$q_nr}"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
//-->
</script>
</head>
<body>
<?php
if ($_SESSION['auth']) {
$tid = $_GET['tid'];
echo "<span id='counter'></span>";
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);
echo "<form name='myForm' action='http://localhost/index.php?option=com_content&view=article&id=51' onsubmit='return validateForm()' method='post'>";
while($row1 = mysql_fetch_array($result1))
{
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
echo "<P><strong>$q_nr $question</strong><BR>";
echo "<img src='images/tests/$pic'>";
echo "<BR>";
echo "<BR>";
echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question_{$q_nr}' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question_{$q_nr}' value='B'>$option2<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='B'>$option2<BR>";
} else {
echo ''; }
} //if q_type <> mr
} //while row1
echo "First name: <input type='text' name='fname'>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
} //else if now > testend
} //if ses auth
?>
why do you think so complicated ?
just use unique ID's for each label, input field, etc ... and check for the innerHTML or value stored; and also you can change the values whenever you want;
btw, your code is messy;
write your code like this:
zone 1: all the js goes here
zone 2: all the html goes here
zone 1:
<script language="text/JavaScript">
//alljs code here
function validate()
{
if( document.getElementById('unique_2').value == null ) document.getElementById('unique_3').innerHTML = 'error1';
if(document.getElementById('unique_4').value == null ) document.getElementById('unique_6').innerHTML = 'error 2';
}
</script>
zone 2:
<form onsubmit="validate()">
all html label and fields go here, like:
<label id="unique_1">label text</label>
<input type="text" id="unique_2" value="" />
<label id="unique_3"></label> this is for the error message
<label id="unique_4">label text</label>
<input type="text" id="unique_5" value="" />
<label id="unique_6"></label> this is for the error message
</form>
This is simple JQuery validation.
I think you must have both validation if you want to be secured...
p.s. Just add 'required' in class of input if you want to be required...
Well for one thing -- validating on the server side is the way to go.
If you just validate on the client side, it can be futzed with (a lot)