PHP/JQuery Dynamic Drop Down not sending variable via POST - php

I have a two step drop down menu. The second select is triggered with jquery. Since I have put this step in, my form does not pass the variable of the selected option. I expect it to just post but it doesn't. I have always had it loaded dynamically, so I didn't think that was the issue. I can't get it to work by passing it through the URL either.
Here is my code:
search.php
while($row = mysqli_fetch_array($result)) { ?>
<option value="<?=$row['id']?>"> <? echo $row['name'] ?></option>
<?php }
<form method = "post" action="index.php?page=users&id=<?php echo $row['id']; ?>">
<p>
<select name="list-select" id="list-select">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
</select>
<?php
echo '<select name="list-target" id="list-target">';
echo '</select>';
$installation_id = $_POST['list-target'];
mysqli_close($con);
?>
users.php:
$installation_id = $_POST['list-target'];
.js
$(document).ready(function($) {
$("#list-select").change(function() {
$("#list-target").load("index.php?page=search&svalue=" + $("#list-select").val());
});
});
Thanks.

Related

Get value of drop down menu and save as variable in PHP

I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>

undefined index error when update button is pressed?

i have here a select option and it work fine if i change the value of the option but when i try not to change anything and i pressed on the update button it gives me an error of undefined error. but if i change the value of a option it successfully updated. I really do not know why its giving me this error..
my code
<?php
include_once 'db.php';
$Reason_for_Deduction = isset($_GET['Summary_of_Reason']) ? $_GET['Reason_for_Deduction'] : '';
if(isset($_POST['update']))
{
$ID = $_GET['ID'];
$Reason_for_Deduction = $_POST['Summary_of_Reason'];
if($LTID->update($ID,$Summary_of_Reason))
{
echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
}
}
if(isset($_GET['ID']))
{
$ID = $_GET['ID'];
extract($LTID->getID($ID));
}
?>
update.php
<select name="Summary_of_Reason" id="yearapproved" class=form-control required/>
<option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>
<option value="18% SLOPE ABOVE AND UNDEVELOPED">18% Slope and Undeveloped</option>
<option value="FIVE (5) HAS. AND BELOW">5 Has. and Below</option>
<option value="CANCELLED TITLE">Cancelled Title</option>
<option value="CANNOT BE LOCATED ON THE GROUND">Cannot Be Located on the Ground</option>
<option value="COMMUNAL FOREST">Communal Forest</option>
<option value="DISTRIBUTED BEFORE CARPER">Distributed Before Carper</option>
<option value="DUPLICATE LH">Duplicate Lh</option>
<option value="ERODED; SILTED/ROCKY NOT SUITABLE TO AGRICULTURAL">Eroded;Silted/Rockt not suitable to Agricultural</option>
<option value="HANDOG TITULO">Handog Titulo</option>
<option value="HOMESTEAD PATENT">Homestead Patent</option>
<option value="IRRIGATION CANAL">Irrigation Canal</option>
<option value="LANDS FOR PUBLIC USE">Lands for Public Use</option>
<option value="LH IS W/N SWAMPY, MANGROVE AREA">Lh is w/n Swampy, Mangrove Area</option>
<option value="LO DIED PRIOR TO CARP">LO Died Prior to CARP</option>
<option value="PASTURE LAND">Pasture Land</option>
<option value="ROAD LOT">Road Lot</option>
<option value="TIMBERLAND">Timberland</option>
<option value="USED FOR INFRASTRUCTURE">Used for Infrastructure</option>
<option value="W/N PROCLAIMED AREA">w/n Proclaimed Area</option>
<option value="W/N DANGER ZONE">w/n Danger Zone</option>
<option value="W/N UNCLASSIFIED PUBLIC FOREST">w/n Unclassified Public Forest</option>
<option value="W/N WATERSHED AREA">w/n Watershed Area</option>
<option value="WORKABLE">Workable</option>
</select>
class.php
public function update($ID,$Summary_of_Reason)
{
try
{
$stmt=$this->db->prepare("UPDATE rlbet SET Summary_of_Reason = :Summary_of_Reason WHERE ID = :ID");
$stmt->bindparam(":Summary_of_Reason",$Summary_of_Reason);
$stmt->bindparam(":ID",$ID);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
You should change this line:
<option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>
to:
<option selected="selected" disabled="disabled" value="<?php echo $Summary_of_Reason; ?>"><?php echo $Summary_of_Reason; ?></option>
You are echoing the value of $Summary_of_Reason inside the option tag and not as its value and the main problem is selected = "true" needs to be changed to selected="selected".
Firstly you have to remove the / from the end of
Also your select box name is "Summary_of_Reason" then why you are using $_GET["Reason_for_Deduction"] in action page, You should use $_GET["Summary_of_Reason"]. "Reason_for_Deduction" is not available in form, this is the reason it gives you undefined index error.

How to use if condition in echo

I have a form to add product to cart and inside it there is a link.
This is link
<?php
echo $this->Html->link('<div class="single-products">'.'<div class="productinfo text-center myimg">'.$this->Html->image("product/".$row["Product"]["photo"],array(/*"width"=>"2500px",*/"height"=>"250px")).'<h2> &#8377 '.$row["Product"]["price"].'</h2>'.'<p>'.$row["Product"]["name"]."</p><a href='javascript:document.ff".($i++).".submit()' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>".'</div>'.'</div>',
array
(
'controller'=>'Public',
'action'=>'singleproduct?id='.$row["Product"]["id"],
),
array
(
'escape'=>false //NOTICE THIS
)
);
?>
And I want to use this code just above Add to cart button
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
}
elseif($row["Product"]["psize"]==2)
{
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
}
?>
This code is working if I put it outside of this HTML helper link, but because of design problem and I want to display it just above the add to cart button
I have tried but couldn't figure out how to put this inside link.
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
} else{
if($row["Product"]["psize"]==2){
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
} else{
echo "TEY IT"
}
}
?>
May Be it gone help.
Bring out your codes in the function $this->Html->link(), assign it to a variable and use if condition. And i think you should use CakePHP Form Helper to output select box.
Example:
<?php
$select = $this->Form->input('psize', array(type => 'select', 'options' => $sizeOptions)); // you can use if conditions here
$link = $this->Html->link('<div>...</div>' . $select . '<div>...</div>', $yourUrlArr);
echo $link;

How to input php echo outside of the php tag

I have this php code that input selected="" at a specific url. I need selected="" to in inserted into my html.
<?php
$uri = $_SERVER['REQUEST_URI'];
if ( strpos($uri,'retailers/amazon/?sort=2') !== false ) {
echo 'selected=""';
} else {
echo 'test';
}
?>
This is the html, I need to insert selected=""... Something like this
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
...
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=desc">Success Rate: High to Low</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=asc">Success Rate: Low to High</option>
<option value="retailers/amazon/?sort=0">Most Comments</option>
</select>
How about this...
<?php
$uri = $_SERVER['REQUEST_URI'];
$sort2 = strpos($uri, 'retailers/amazon/?sort=2');
?>
<option <?php echo ($sort2 === false? '' : 'selected=""'); ?> value="retailers/amazon/?sort=2">Newest to Oldest</option>
I assume you are trying to access the echo command outside the php tag and somewhere in html
it is not possible but there is an solution for this.
so you can try this option.
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="?php echo $uri?" </option>
</select>
its mean you are using the php code within the html area and you can access every variable of php by this.

Select form option on page load

I would like to select the option contained within a php variable on page load.
<?php
$pre = 78;
?>
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78">78</option>
<option value="82">82</option>
</select>
<script>
$(document).ready(function(){
$("form8").val("<?php echo $pre; ?>");
}
</script>
Expected output should be,
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78" selected="selected">78</option>
<option value="82">82</option>
</select>
http://jsfiddle.net/qQZVN/1/
Example
$('#form8').val("<?php echo $pre; ?>") ;
Why not just use HTML to set selected='selected'? Seems easier, and supports non-JS users.
$(document).ready(function(){
$("#form8").val(<?php echo $pre; ?>);
}
$('select#form8').val('<?php echo $pre; ?>');

Categories