HTML form POST data not transferring to next page - php

I have a form with a drop-down box. The user selects an option from the dropdown box and hits submit. On submit, a new page opens up with a new form. The option that was selected is not being transferred to the next page. My form code is:
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname" type="input">
<option></option>
<?php foreach ($data as $row): ?>
<option>
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="heroname">Submit</button>
</div>
</form>
The next page loads and displays some test variables at the top, but the variable from the POST is empty. My code on the page is
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include('header.php');
$username = $_SESSION['username'];
echo $_POST['heroname'];
echo $username;
$test1 = "test 1, before the if statement";
echo $test1;
I get no errors on the page. The username variable and the test1 variable echo normally. The heroname variable doesn't. I need help figuring out why the selection from the form is not transferring to the next page. Thanks in advance.

You have forgotten the value attribute from the <option> elements. Whatever is in the value attribute is what is returned to the server script.
You have a type="input" attribute on the <select> element, that does not belong there.
You have called the button <button type="submit" name="heroname"> which is also what you called the dropdown <select name="heroname"> you cannot use the same name more than once. So change the button name to something else like <button type="submit" name="submithero"> for example
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="submithero">Submit</button>
</div>
</form>

the button does not need a name attribute
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>

First, I removed the value attribute from select. Second, I added a value attribute to option. Third, I changed the name for the button to match the IF statement on the next page. The heroname is now being passed to the next page. I have a whole new set of errors now about undefied index, which I will research first, and if needed, post a new question. Thank you all for the help!!

Related

dompdf form submit didn't send data codeigniter

i am trying to create an PDF using Dompdf, in my program i have model query with a dynamic parameter based on the input in the form, but when i click on my button to send the value from the select button to the controller, the data not been send properly, any idea why? below are my code
<form class="form" method="get" action="<?=site_url()?>/laporan/pdfdownload" id="myID" name="myName">
<select id="sel1" class="form-control">
<option disabled selected="selected">Pilih</option>
<?php foreach ($kerja as $rows){?>
<option value="<?php echo $rows->id_project?>"><?php echo $rows->id_project.' - '.$rows->nama_project ?></option>
<?php }?>
</select>
<select id="sel2" class="form-control">
<option disabled selected="selected">Pilih</option>
<?php foreach ($item as $rows){?>
<option value="<?php echo $rows->id_project?>"><?php echo $rows->id_project.' - '.$rows->nama_project ?></option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px;margin-left: 28px;width: auto" name="filter_button" type="submit" class="form btn btn-danger"><i class="fa fa-search"></i>&nbspSearch</button>
</form>
and here is my controller
public function pdfdownload(){
//If i click submit then all of the post didnt get sended
$one = $this->input->post('sel1');//no value at all, anyone know why?
$two = $this->input->post('sel2');//no value at all, anyone know why?
$data['real'] = $this->report_m($one,$two)->row();
htmlcontent = $this->load->view('laporan/download/laporan3.php',$data,true);
include(APPPATH."third_party/dompdf/autoload.inc.php");
// require_once APPPATH . 'third_party/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf();
$dompdf->load_html($htmlcontent);
$dompdf->set_paper("f4");
$dompdf->render();
$dompdf->stream("cobadlu.pdf",array("Attachment" => false));
exit(0);
}
enter code here
In your HTML form, you need to use name attribute, and not only ID :
<select id="sel2" name="sel2" class="form-control">
First of all there is a mistake in your form:
<form class="form" method="get" action="<?=site_url()?>/laporan/pdfdownload" id="myID" name="myName">
You given the method='get' here, please change it to 'post'.
Second you are not include the 'name' attribute to the input type so after submit no data will send. So add the name attribute like that:
<select id="sel1" name="sel1" class="form-control">
<select id="sel2" name="sel2" class="form-control">

retrieve value from drop down bar and display result

on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks

Handling isset and select box in php html and for loop

Here is my code. This needs some optimization if you think the below code is bad and someone can really help me
I'm using two if condition and two else condition. Is there anyway this can be fine-tuned.
In the search box i need to retain the previous selected book name. i.e when the submit button is clicked the select box should display the result with the
selected item in the select box. for ex: if in dropdown if i select "book1" and click the submit button, the result should be displayed along with the "book1" as default inside the select box. in my case, the search box is getting reset on each submit click.
My Code:
<?php
if(isset($_POST['submit'])){
$get_project_id = $_POST['project_name'];
if($get_project_id == 1){
..display all the books(mysql query)..
}else{
..display the particular book(mysql query)..
}
}else{
..if the search button is not clicked then display all the books(mysql query)..
}
?>
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Any help will be appreciable.
Than,ks
Kimz
You can do it by checking the condition inside while loop. Checking the value of option and the post value are same or not as shown in the below code.
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option <?php if(isset($_POST['book_list']) && ($_POST['book_list'] == $row['Project_Auto_Id'])) { echo 'selected'; } ?> value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Not sure if there is any better way to do the two if... else statements but even if there is there's nothing wrong with the way you've done it, just a different way.
With regards to having the option selected after submitting you need to change your output of the options to this:
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>"<?php if(isset($_POST['book_list']) && $_POST['book_list'] == $row['Project_Auto_Id']) echo ' selected'; ?>>
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>

Session not passing dropdown selected element to other php script

First php script -
<?php
session_start();
?>
<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
<?php
?>
<option value > Select Feature</option>
<?php
foreach($newFeature as $feat)
{
?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
<?php
$_SESSION['feature'] = $feature;
?>
second php script -
<?php
session_start();
echo $_SESSION['feature'];
?>
When I run second php script, I get Array as echoed element instead of the element I selected.
What is wrong with the logic here ?
Please guide.
You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.
<form method = "post">
<select name="feature" id="feature">
<option value > Select Feature</option>
<?php foreach($newFeature as $feat) : ?>
<option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>
When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.
session_start();
$_SESSION['feature'] = $_POST['feature'];
After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):
if (isset($_POST['mySubmit'])){
$_SESSION['feature'] = $_POST['feature'];
}

Remember the last selection on Simple form

Is it possible that when
I select something from the list
And I click Go
Browser will remember my last choice from the list
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<option value="">select</option>
<option value="client">table client</option>
<option value="user">table user</option>
</select>
<input name="submit" type="submit" value="go" />
</form>
For example,
if I chose from the list user table
And hit Go
He will remain there
you can use sessions to access some data at a later time on a different script:
session_start();
$_SESSION = $POST['list'];
now the selected item from the list will remain there, as long as the browser is still opened.
if you want to remove the item from the session use:
unset( $_SESSION['list'] );
and remember every time you use sessions you must use at the top of the file (always):
session_start();
In order to make a select "stick" you need to put the word selected inside the <option> tag before the value. In order to do that you’re going to need to dynamically generate the options using a foreach loop. Inside the loop you’re going to look at the $_REQUEST and if it matches the value of what is being iterated in the loop you echo selected.
I’ve done this a dozen or more times and it works perfectly.
<?php
$options = array("select", "client", "user");
?>
Then in the page:
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<?php foreach ($options as $option): ?>
<option <?php if ($_GET['list'] == $option) { echo "selected"; } ?> value="<?php echo $option; ?>"><?php echo $option;?></option>
<?php endforeach ?>
</select>
<input name="submit" type="submit" value="go" />
</form>

Categories