PHP drop down and Session question - php

What really needs to happen here is.. A session takes my data from the drop down listed below. Its gonna take it to the process page then bring it back to this page with a header on the process page. Then when it gets back to the first page the dropdown will populate with the session or the same client as when I left that page and the on change of the drop down will work. Can anyone help me with this. Thank you Right now this returns the client to the drop down but the onchange for the drop down does not work which is what I really need to work.
session_start();
$current = isset($_SESSION['ClientNamefour']) ? $_SESSION['ClientNamefour'] : 0;
while ($row = mysql_fetch_array($result)) {
$id = $row["Client_Code"];
$thing = $row["Client_Full_Name"];
$value = "$id, $thing";
$sel=($id==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">".$thing;
}
?>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="ClientNamefour" onchange="this.form.submit()">
<option value=0>Client
<?php echo $options4?>
</select>
</form>
Process page header
session_start();
$_SESSION['ClientNamefour'] = $_POST['txtclientcode'];
// Do the redirect
// Do the redirect
header("Location: test.php");
exit();

I don't know your question or problem but I just guess that it should be
$_SESSION['ClientNamefour'] = $_POST['ClientNamefour'];
instead of
$_SESSION['ClientNamefour'] = $_POST['txtclientcode'];
Also don't forget to put closing </option> tags into your code.
Of course you have to send the the form data to the process page, but
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
looks like the data gets send back to the same page.
And never trust data submitted by the user.
Edit:
Try:
onChange="Javascript:document.forms[0].submit()"
or
onChange="Javascript:document.forms['form'].submit()"
But that is only what I found using Google.

Related

Cannot seem to use $_POST twice on the same page

I am trying to access different elements in an XML so I can edit them or add new ones from within a web page. I'm using PHP to loop through the parent tags and display in a select list then have submit button to POST the element selected - no issue.
I then use PHP to find all the names of the selected elements children, and display those names in another select list form. This displays the children in the list correctly however when I hit submit it displays correctly if I echo $_POST("Section") but clears the previous $_POST("Page").
I think this has something to do with using action="", but interestingly if I change one them to GET it works as intended. I actually want a 3rd step into lower children again, so cannot use that solution as a dodgy workaround.
I wont post XML as it has sensitive data in it and you can just trust I am stepping through that fine, it's the php clearing $_POST that is the big issue for me.
Please go easy - I literally learnt how to make a basic html webpage in april.
Tried a bunch of different form action including the .php it is on, and also
<?php echo $_SERVER['PHP_SELF']; ?>" >
As stated before - using one as GET and one POST seems to solve the issue
<?php
//pages processing and put into an array
$pages_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
array_push($pages_array, $page->name);
}
$pagecount = count($pages_array);
?>
<form name="page_select" method="POST" action="">
<select name="Page">
<?php
for ($i = 0; $i < $pagecount; $i++){
print '<option>'.$pages_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_page" value="edit"/>
</form>
<br></br>
<?php
// save page selected into $pg and display page being edited
$pg = $_POST["Page"];
echo 'YOU ARE EDITTING PAGE: '.$pg;
?>
****** THEN FURTHER DOWN THE PAGE *****
<?php
// section processing
$section_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
if($page->name == $pg){
foreach($page->sections->section as $section){
array_push($section_array, $section->name);
}
}
}
$sectioncount = count($section_array);
?>
<form name="section_select" method="POST" action="">
<select name="Section">
<?php
for ($i = 0; $i < $sectioncount; $i++){
print '<option>'.$section_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_section" value="edit"/>
</form>
<br></br>
<?php
$sct = $_POST["Section"];
echo 'YOU ARE EDITTING SECTION: '.$sct;
?>
I want output to remember both arrays after the second form is submitted, however due to the first POST variable being wiped it means I also lose the second array since when it gets back to pushing to $section_array $page->name can never = $pg since $pg is now empty
Every time you submit a form, the page is reloaded and PHP will only have access to the POST variables from the form that triggered the page reload. This means that as your second form is submitted, $_POST['Page'] ceases to exist.
There are many ways around this problem, here is a fairly simple one. In your second form, create a hidden input that contains the value of the $_POST['Page'] variable. That way, when the second form is submitted, the Page value will be propagated to the next step of the process. Adding this code to your section_select form should suffice:
<?php
if(isset($_POST['Page'])) {
?>
Page: <?php echo $_POST['Page'] ?>
<input type="hidden" name="Page" value="<?php $_POST['Page'] ?>" />
<?php
}
?>

HTML form: action clarification

Hello I am currently making a registration page,
I'm new to php, and was wondering if this would work when I use the action part in form
<form method="post" id="formArea" action="action.php">
action.php is a file that contains
redirect.html
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
would this work? or is there a way where I can have both in the action section without the need for having to make a new file?
You Could Just Do This From The Form. If This is what your looking for:
<form method="post" id="formArea" action="<?PHP $_SERVER['PHP_SELF'];?>">
//this will make the form post to same page as the form
if your wanting to send this to both you can save the $_POST Data In A Variable and Use It Later.
EX:
<?PHP
//your form submit code might look something like this same file as your form
if(isset($_POST['submit']))
{
foreach($_POST as $key -> $value)
{
//echo or show your values or store your values in database or w/e
$_SESSION['post'][$key] = $value;
//this will make a session variable to be used again later each session array is marked with post identified by $key and stored a $value
}
//you can call your other page here this will only run after this page runs and loads your information above and then redirect with a session variable.
header("Location: redirect.html");
}
else
{
echo "<form method=\"post\" id=\"formArea\" action=\"".$_SERVER['PHP_SELF']."\">";
}
This Code Is Not Tested But It Would Work the redirect.html will process code from $_SESSION['post']

How to go back to a page that was loaded with a submited form?

A user submit a form and goes from page_0.php to page_1.php. Then goes through a link from page_1.php to page_2.php. If the user want to press the "back" button from the browser, How is it possible to prevent a warning message from the browser like "Error code: ERR_CACHE_MISS" (confirm form submit?) and also load the same data that was displayed before pressing the link to page_2?
I tried using session, but not always work:
page_0.php: the user selects an option:
<!-- this is page_0.php -->
<form action="page_1.php" method="post">
<select name="id">
<option value="1">Car</option>
<option value="2">Bike</option>
<option value="3">Skate</option>
<option value="4">Plane</option>
</select>
<button type="submit">Submit</button>
</form>
page_1.php: (after submit the form located on page_0.php, the user arrives to page_1.php)
<?php
session_start();
//this is page_1.php
if(isset($_POST['id']))
{
$product_id = $_POST['id'];
$_SESSION['data'] = $_POST['id'];
}
else
{
$product_id = $_SESSION['data'];
}
$result = mysqli_query($con, "SELECT id_product_name, product_name FROM products WHERE id='$product_id'");
$row = mysqli_fetch_assoc($result);
while ($row=mysqli_fetch_array($result))
{?>
Go to product detail
<?php
}
?>
Now the user clicks on one of the links to see the further details of a product. Let say that clicks on the link with id=3:
Go to product detail
The user arrives to page_2.php.
<?php
//this is page_2.php
$product_id = $_GET['id'];
echo 'Hello here you can find details of product'.$product_id;
?>
Now the user press the "back" button on the browser.
My question is: How can the user go back to page_1.php and still view the same information that he was seeing before clicking on the link and also without getting any message from the browser?
You should always redirect serverside from a page that handles a form submission, so it should not possible to get back to it from the back button. This is to prevent the possibility of accidental double submission on back button, as well as ugly errors like the one you noticed.
(Just to clarify. What this does is make the history skip the page that handles the submission, so that hitting back from page_2 will take you directly back to the form, not the handling page.)
So, change the end of your page_1.php from
?>
Go to page 2
To:
header('Location: page_2.php');
exit;
Also, for this to work, you may have to make sure not to print anything out before the call to header(). I don't remember if that's necessary in PHP or not, but it is in some languages (e.g. Java/JSP).
Echo the session variable in the form's input value.
Sidenotes:
It's a bit unclear as to which page number corresponds to what, so I will leave that to your discretion
Make sure there is no output before any of the pages.
Part 1
<?php
session_start();
?>
<form action="page_1.php" method="post">
<input value="<?php echo $_SESSION['data']; ?>" name="id" />
<button type="submit">Submit</button>
</form>
If that isn't the desired result, then just use the session variable on the same page.
I.e.:
$data = $_SESSION['data'];
echo $data;
and keeping your present input as <input value="854" name="id" />
Part 2
<?php
session_start();
if(isset($_POST['id']))
{
$product_id = $_POST['id'];
$_SESSION['data'] = $_POST['id'];
}
else
{
$product_id = $_SESSION['data'];
$result = mysqli_query($con, "SELECT product_name FROM products WHERE id='$product_id'");
$row = mysqli_fetch_assoc($result);
echo $row['product_name'];
}
?>
Go to page 2
Edit:
Page 2 would look something like this, assigning $_SESSION['data'] to $_GET['id']:
<?php
//this is page_2.php
session_start();
$_GET['id'] = $_SESSION['data'];
$product_id = $_GET['id'];
echo 'Hello here you can find details of product: '.$product_id;
?>

Reset dropbox automatically to first value after the users hit submit button PHP

After the user hits the submit button, how do I reset the drop down menu to the "blank" option of the the menu? I am using a MVC set up with php and HTML, and the concrete5 library. THANKS IN ADVANCE! Here is what I have so far:
Controller code:
public function authorize() {
$selectHost = array('' => '');
foreach ($this->host->getHostInfo() as $row) {
if (isset($row['HARDWARE_id'])) {
$selectHost[$row['id']] = $row['host'];
}
}
$this->set('selectHost',$selectHost);
$postCheck=array(array('param' => 'host',
'check' => '^[0-9]{1,50}$',
'error_msg' => 'Invalid Host ID'),
);
$post = scrub($_POST,$postCheck);
if (isset($post['host'])) {
$this->host->authorize($post['host']);
$this->set('test', "<p> The host has successfully been authorized.</p>");
}
else{
$this->set('failed', "<p>Invalid Host ID</p>");
}
}
view code:
<form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>">
<?php
$form = Loader::helper('form');
print $form->label('host', 'Host: ');
print $form->select('host', $selectHost);
?>
<?php
print $form->submit('Submit','Submit');
echo $test;
echo $failed;
?>
</form>
I'm pretty positive that there's no way to override C5's desire to take the POSTed value and use that as the default. Even if, as TWR suggested, you specify a value. (This is typically a good thing, because if the page is POSTed to and there's an error, you don't want to show the value from the database; you want to show what was in the POST).
You can override the form helper pretty easily.
However, I'd suggest that you do a redirect after successful submission (don't redirect after an error -- then the POSTed value will be useful) to a page. You can redirect to another page, or the same one, ideally with a confirmation message. See https://github.com/concrete5/concrete5/blob/master/web/concrete/core/controllers/single_pages/dashboard/blocks/stacks.php#L23 for an example of using redirect.
This is the best practice for your problem but also because, with your current code, if somebody hits refresh, it'll rePOST the data and reauthorize the host.
i think you could extend the form tag with a (javascript) onsubmit action which does the reset.
Since it's a form submit, you just want to change the value of the "drop box"/select in your view. After a submit, you'll have a fresh page load; so, in every case you'll want to display the default value, and not the current value of $selectHost
In your view, change this
print $form->select('host', $selectHost);
to this
print $form->select('host', $selectHost, null);
According to http://www.concrete5.org/documentation/developers/forms/standard-widgets
If the problem is that the Concrete5 form helpers are not behaving as you want, then just don't use them -- instead just use regular HTML form inputs instead.
<form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>">
<label for="host">Host: </label>
<select id="host" name="host">
<?php foreach ($selectHost as $value => $text): ?>
<option value="<?php echo htmlentities($value); ?>"><?php echo htmlentities($text); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Submit" />
<?php
echo $test;
echo $failed;
?>
</form>

PHP dropdown sticky

I have a database populated drop down list.
$options4="";
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$options4.="<OPTION VALUE=\"$id, $thing\">".$thing;
}
?>
<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT NAME="ClientNamefour" OnChange="this.form.submit()">
<OPTION VALUE=0>Client
<?php echo $options4?>
</SELECT>
</FORM>
Once this changes it pulls a client name from a database and brings up some information there is a form that can be filled out to insert more information into a database. Once that form is filled out this page sends the information to another page with the code to insert the data into the database. Then I am using a header at the end of that code to send it back to this page. But when it comes back to this page I want it to comeback to the client they selected before and not a blank screen. So how do I make a DB populated list automatically comeback to the previous selection using php?
All you need to do is modify the way you construct the options to spot when you are creating the selected one, then add a SELECTED attribute to it...
//obtain current value from GET, POST or COOKIE value...
$current=isset($_REQUEST['ClientNamefour'])?$_REQUEST['ClientNamefour']:'';
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$value="$id, $thing";
//figure out if we should select this option...
$sel=($value==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">".$thing;
}
One way to keep the value persistent would be to store it in the session, e.g. when you process the form
//assuming session_start() has been called...
if (isset($_POST['ClientNamefour']))
$_SESSION['ClientNamefour']=$_POST['ClientNamefour'];

Categories