$_POST not working with <select> - php

I can get the $_POST working perfectly with the inputs but not with the select statements for some reason. Here is my code:
registration.php
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="<?php if (isset($user_type)) { echo 'admin'; } ?>">Faculty</option>
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="submit" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
results.php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Setting the form inputs to variables
$first_name = trim($_POST["first_name"]);
$last_name = trim($_POST["last_name"]);
$email_address = trim($_POST["email_address"]);
if (isset($_POST["submit"])) {
$user_type = $_POST['user_type'];
echo $user_type . '<br><br>';
}
echo $first_name . '<br><br>';
echo $last_name . '<br><br>';
echo $email_address . '<br><br>';
}
ouput:
Alex
Cory
alex#mydomain.com
Any Ideas as to why this $_POST['user_type']; isn't working?

Assuming that what you have posted is the complete contents of registration.php then the issue is that your value for each of the user_name options contains an empty string.
The actual HTML that your script generates is:
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="">Faculty</option>
<option value="">Student</option>
</select>
This is happening because your registration.php script is checking if the PHP variable $user_data is set before adding the values of admin or registeredUser to the <option> tags.
If we expand your code you can see that the if condition will never fire:
<option selected="" value="<?php
if (isset($user_type)) {
// This will never be fired because the PHP variable $user_data is not set.
echo 'admin';
}
// The result is that nothing is echoed so the HTML reads value=""
?>">Faculty</option>
To fix, then either remove the conditional check:
<select id="user_type" name='user_type' class="form-control">
<option selected="selected" value="admin">Faculty</option>
<option value="registeredUser">Student</option>
</select>
Or set $user_data at the start of the script:
<?php $user_data = true; ?>
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
...
But what I think you are really trying to achieve is to determine which of these options to populate as selected in your form. If this is the case, you should check that $user_type is set and if it's value matches the option value, mark that option as selected.
<select id="user_type" name='user_type' class="form-control">
<option <?php
if (isset($user_type) && $user_type === 'admin') {
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser') {
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>

I cant see the submit button in your code, does it have name='submit' on it ?
Additionally if you are using chrome inspect the post in network(developer tools) to see if 'submit' is posted

This happens because both of your options under select id="user_type" have no values when registration.php is executed.
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
If $user_type is not defined the value for both options will be "" (empty string) - and I assume this is what happens.

Here's the Answer
The problem had a little to do with PHP and also a little to do with HTML. PassKit did a great job explaining the PHP which I ended up using in my code to make it work properly. Look to his answer about the PHP and mine about the HTML.
The opening < form > tag needed an "id" attribute that matched the < select > tag's "form" attribute which neither of them had.
Solution:
<form id="user-form" class="form-horizontal" action="results.php" method="post">
right here -^ needed: id="someFormID"
<!-- Faculty or Student? DROP-DOWN -->
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select form="user-form" id="user_type" name='user_type' class="form-control">
right here ---------------------^ needed: form="someFormID"
<option <?php
if (isset($user_type) && $user_type === 'admin')
{
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser')
{
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="singlebutton" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>

use submit button in your form or check post with if($_POST)

Related

How to maintain the PHP GET value on the address bar if I use another GET?

I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>

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">

keep selected values in select box field after search

I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.

Why am I unable to access form data when submitting using OnChange?

I have a simple HTML select option in which it should automatically send OnChange. The form seems to send or "Reload the page" should I say. I am not getting ANY data the other end. I have tried POST and GET I have renamed every section of the forms data and changed the entire if(isset() information. Any ideas why I am not getting any data?
FORM:
<?php
$system1 = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT * FROM ap_system_status WHERE system_id = '1'"));
$system_id_1 = $system1['system_id'];
$system_name_1 = $system1['system_name'];
$system_status_1 = $system1['system_status'];
?>
<strong><? echo $system_name_1; ?></strong>
<div align="right" style="float:right">
<div class="form-group">
<form action="" method="GET" id="system_status" name="system_status">
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" />
<select class="form-control" onchange="this.form.submit()" id="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors'>Experiencing Errors</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Being Updated'>Being Updated</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Maintenance Mode'>Maintenance Mode</option>
<option value='<i class="fa fa-times-circle-o" style="color:#F00"></i> System Offline'>System Offline</option>
<option value='<i class="fa fa-check-circle-o" style="color:#090"></i> Good Service'>Good Service</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
DATA COLLECTION (Very basic but only testing!!):
<?php
if(isset($_POST['system_status'])){
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
}else{
}
?>
A couple of changes to your form and PHP should get this working. In order for the form elements to be submitted with the form, they must have name attributes, and the form method must match the superglobal ($_GET or $_POST) you are accessing in PHP.
<!--Change the form method to match what you are using in your PHP-->
<form action="" method="POST" id="system_status" name="system_status">
<!--Add a name to this input-->
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" name="system_id" />
<!--Add a name to this select-->
<select class="form-control" onchange="this.form.submit()" id="status" name="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<!--Not related to your question, but switch the value attribute with the
option text so you can see your icons and get the value I assume you expect-->
<option value="Experiencing Errors"><i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors</option>
...
</select>
<noscript><input type="submit" value="Submit"></noscript>
I would actually not recommend getting rid of the isset check altogether, but modifying it so that it actually verifies that the values you are going to use are present.
<?php
if (!empty($_POST['system_id']) && !empty($_POST['status'])) {
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
} else {
// error handling for missing values
}

display the output in the same page using php

im new to php.i created a page in php containing chk boxes ,input fields and retrive the data from the database(mysql) and also use the javascript.
i h've a problem in this ,that is i want to display the output in the same page.
i created the code like this,i want the output in same page,but it shows error.
<?php
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","demosdef_review","review123");
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("demosdef_review", $con);
$state = $_POST["state"];
$country = $_POST['country'];
$contry = "USA";
if($country==1)
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='1' AND id='$state'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
else
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='$country'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
}
?>
enter code here
<div class=buy>
Dealer Enquiry
</div><br/>
<div class=buyfont>Authorized Retailers</div>
<div><img src="images/archivers.png"><br/>
<a href="http://www.archiversannex.com/Books-And-SoftwareSoftware-And-Accessoriesdefault.aspx?PageID=20&CategoryID=48"/>
<img src="images/logo_archivers_annex.png" ></a><br/><br/>
<br/><br/>
</div><br/>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
<p>
<label for="country" style="padding-right: 2px;">Country</label><select name="country" value="countryid"
id="countryid"
onchange="sub()" style="width:120px;">
<option value="1">USA</option>
<option value="2">CANADA</option>
<option value="3">UK</option>
<option value="4">AUSTRALIA</option>
<option value="5">ITALY</option>
<option value="6">GUATEMALA</option>
<option value="7">NEW ZEALAND</option></select></p>
<p>
<label for="state" style="padding-right: 14px;">State</label>
<select id="state" name="state" value="state" style="width:120px">
<option value="7">Alabama</option>
<option value="8">Alaska</option>
<option value="9">Arizona</option>
<option value="10">Arkansas</option>
<option value="11">California</option>
<option value="12">Colorado</option>
<option value="13">Connecticut</option>
<option value="43">Florida</option>
<option value="14">Georgia</option>
<option value="15">Idaho</option>
<option value="16">Illinois</option>
</select></p> <br/>
<input class="dealer-submit" type="submit" value="Submit" onClick="buy_func()"/>
</form>
<script type="text/javascript">
function sub()
{
var x=document.getElementById("countryid").selectedIndex;
var y=document.getElementById("countryid").options;
var z = document.forms[0].state;
if(y[x].index==0){
z.disabled = false;}
else if(y[x].index>0) {
z.disabled = true;}}
</script>
Your HTML markup has an error
<a href="http://www.hobbylobby.com/storelocator"/>
It should be
<a href="http://www.hobbylobby.com/storelocator">
Also try
<form method="POST" action="">
Keeping the action blank will post it to the same page itself
And replace your submit button with this
<input class="dealer-submit" type="submit" name="submit" value="Submit" onClick="buy_func()"/>
Check the value of the submit button, if its set isset($_GET('submit')) then display the results
does that solve your problem?
submit is the type of the element, 'Submit' is the value you are looking for:
<?php
if(isset($_POST['Submit']){
}
?>
Remove the backslash at the end of the line:
<a href="http://www.hobbylobby.com/storelocator"/>
^

Categories