How do I pass the id value to the function? - php

How do I pass the id value to the function?
What I am doing is, When the user changes the dropdown then it will get the value of dropdown and also get the input value which is hidden inside foreach.
I tried from my side I am getting on change the value from the dropdown but how do I get the input value in the jquery? now I am getting the last value of the input field.
$x=1;
foreach ($duration as $row) {?>
<input type="hidden" name="activeID" id="activeID<?php echo $x;?>" value="<?php echo $row->activity_name_id;?>">
<select name="Duration<?php echo $x;?>" class="form-control" id="Duration<?php echo $x;?>">
<option selected disabled >Select year</option>
<option value="12m" <?php if($row->Duration == '12m' ){ echo 'selected="selected"'; } ?>>1 Year</option>
<option value="6m" <?php if($row->Duration == '6m' ){ echo 'selected="selected"'; } ?>>6 months</option>
</select>
<div id="results<?php echo $x;?>"></div>
<?php $x++;}
jquery
$('[id^="Duration"]').change(function () {
var a=$('input[id^="activeID"]').attr('id');// how to I display the input id here
alert(a);
var value = $('input[id^="activityID"]').val();// i tried this one as well
alert(value);
var end = this.value;
alert(end);//getting drodpown id value
});

You can put your input and select inside a div. When the dropdowns are changed, get the input sibling
<?php
$x=1;
foreach ($duration as $row) {
?>
<div>
//Your input and select here
</div>
<?php } ?>
<script>
$('[id^="Duration"]').change(function () {
var input = $($(this).siblings("input")[0]); // get the input element
var id = input.attr("id"); //id of the input
var value = input.val(); //value of the input
alert(value);
});
</script>

One thing I have observed in your code:
$x=1;
foreach ($duration as $row) {
<input type="hidden" name="activeID" id="activeID<?php echo $x;?>" value="<?php echo $row->activity_name_id;?>">
<select name="Duration<?php echo $x;?>" class="form-control" id="Duration<?php echo $x;?>">
<option selected disabled >Select year</option>
<option value="12m" <?php if($row->Duration == '12m' ){ echo 'selected="selected"'; } ?>>1 Year</option>
<option value="6m" <?php if($row->Duration == '6m' ){ echo 'selected="selected"'; } ?>>6 months</option>
</select>
<div id="results<?php echo $x;?>"></div>
}
You are not at all increasing $x anywhere and hence each and every hidden input tag is getting id = 'activeId1'

<?php
$x=1;
foreach ($duration as $row) {
?>
<input type="hidden" name="activeID" id="activeID<?php echo $x;?>" value="<?php echo $row->activity_name_id;?>">
<select name="Duration<?php echo $x;?>" class="form-control DurationClass" id="Duration<?php echo $x;?>">
<option selected disabled >Select year</option>
<option value="12m" <?php if($row->Duration == '12m' ){ echo 'selected="selected"'; } ?>>1 Year</option>
<option value="6m" <?php if($row->Duration == '6m' ){ echo 'selected="selected"'; } ?>>6 months</option>
</select>
<div id="results<?php echo $x;?>"></div>
<?php } ?>
<script>
$('.DurationClass').change(function () {
var a=$('input[id^="activeID"]').attr('id');
var selectID=$(this).attr('id');
//alert(a);
var value = $(this).val(); // in loop every dropdown select current option value return
alert(value);
});
</script>

You can use onchange event in your HTML part of the code
$x=1;
foreach ($duration as $row) {?>
<input type="hidden" class="activeID" name="activeID" id="activeID<?php echo $x;?>" value="<?php echo $row->activity_name_id;?>">
<select name="Duration<?php echo $x;?>" onchange="myfunction();" class="form-control" id="Duration<?php echo $x;?>">
<option selected disabled >Select year</option>
<option value="12m" <?php if($row->Duration == '12m' ){ echo 'selected="selected"'; } ?>>1 Year</option>
<option value="6m" <?php if($row->Duration == '6m' ){ echo 'selected="selected"'; } ?>>6 months</option>
</select>
<div id="results<?php echo $x;?>"></div>
<?php $x++;}
Here is the function
function myfunction () {
alert($(this).parent()); //what are you getting
alert($(this).parent().find(".activeID").value()); //what are you getting?
alert("Id of the select box is "+ this.id);
alert("Value of the select box is "+this.value );
};

$(".select").change(function(){
alert("Id of the select box is "+ this.id);
alert("Value of the select box is "+this.value );
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Form control: select</h2>
<p>The form below contains two dropdown menus (select lists):</p>
<form>
<div class="form-group">
<label for="sel1">Select list (select one):</label>
<select class="form-control select" id="Sell">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<label for="sel1-item">Select list sell item (select one):</label>
<select class="form-control select" id="Sell-item">
<option>car</option>
<option>bike</option>
<option>cycle</option>
<option>auto</option>
</select>
</div>
</form>
</div>
</body>
</html>

Related

Autofill dropdown list from URL

I have a registration form where I want to be able to get the account plan users have selected form another page and will autofill my form for account type.
The a href from the pricing site
signup.php?type=free
signup.php?type=pro
signup.php?type=plus
Code for the form in registration site
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option value="free">Free</option>
<option value="pro">Pro</option>
<option value="plus">Plus</option>
</select>
</div></form>
This will get the $_GET['type'] variable & set selected attribute for the drop down! You could also do it with jQuery (seen below)
<?php
if(isset($_GET['type'])){
$type = $_GET['type'];
}
?>
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option <?php if($type == 'free'){ echo "selected='selected'"; } ?> value="free">Free</option>
<option <?php if($type == 'pro'){ echo "selected='selected'"; } ?> value="pro">Pro</option>
<option <?php if($type == 'plus'){ echo "selected='selected'"; } ?> value="plus">Plus</option>
</select>
</div></form>
jQuery...
<?php if(isset($_GET['type'])){ ?>
<script>
$(document).ready(function(){
var type = "<?php echo $_GET['type'] ?>";
$("#type > option").each(function(){
if($(this).val() == type){
$(this).attr("selected", "selected")
}
})
})
</script>
<?php } ?>

PHP submit automatically to one page and manually to another

Rajesh I succeeded to submit the page using javascript onclick event like you told me but now for some reason the variables don’t get submitted to the javascript redirect page.
Can you tell me what I am doing wrong?
Thanks a lot for your help.
Here is my new code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<!--For dropdown auto submit-->
<script>function onSelectChange(){
document.getElementById("myselect").submit();
}</script>
<!--For final page submit-->
<script>
function submit() {
window.location = "action_page.php";
}
</script>
</head>
<body>
<form action="test.php" method="post">
<select name="s1" style="width: 70px;" id="mySelect" onchange="this.form.submit()" </select>
<optgroup label="170011 Non-Isolated RS-422">
<option value="">Select...</option>
<option value="170011-3" <?php if ($_POST['s1'] == '170011-3') echo 'selected="selected"'; ?> >0011-3v3</option>
<option value="170011-5" <?php if ($_POST['s1'] == '170011-5') echo 'selected="selected"'; ?> >0011-5v0</option>
<optgroup label="170013 Isolated LVTTL Input">
<option value="">Select...</option>
<option value="170013-3" <?php if ($_POST['s1'] == '170013-3') echo 'selected="selected"'; ?> >0013-3v3</option>
<option value="170013-5" <?php if ($_POST['s1'] == '170013-5') echo 'selected="selected"'; ?> >0013-5v0</option>
<optgroup label="170015 Isolated LVDS Input">
<option value="">Select...</option>
<option value="170015-3" <?php if ($_POST['s1'] == '170015-3') echo 'selected="selected"'; ?> >0015-3v3</option>
<option value="170015-5" <?php if ($_POST['s1'] == '170015-5') echo 'selected="selected"'; ?> >0015-5v0</option>
<optgroup label="170016 Isolated RS-422 Input">
<option value="">Select...</option>
<option value="170016-3" <?php if ($_POST['s1'] == '170016-3') echo 'selected="selected"'; ?> >0016-3v3</option>
<option value="170016-5" <?php if ($_POST['s1'] == '170016-5') echo 'selected="selected"'; ?> >0016-5v0</option>
<optgroup label="170017 Non-Isolated LVTTL Input">
<option value="">Select...</option>
<option value="170017-3" <?php if ($_POST['s1'] == '170017-3') echo 'selected="selected"'; ?> >0017-3v3</option>
<option value="170017-5" <?php if ($_POST['s1'] == '170017-5') echo 'selected="selected"'; ?> >0017-5v0</option>
</select></form>
<!--<input type="submit" id="form_page" value="Submit">-->
<input type="submit" value="Submit Final" onclick="submit()">
</body>
</html>
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). You should give the different id to form tag and button tag.
You've gave same id in form tag and button tag. You've take two form tags and you've taken Submit button instead simple button. So html document only consider first form tag so when you click on button you will simply redirect to test.php
Now for redirect to another url using button you can use javascript onclick event like:
<input type="button" value="Submit" onclick="yourfunction">
and in function redirect using javascript like:
window.location = "http://www.example.com";

How to get value of select option an used in php variable?

I'm trying to take the value of SELECT option and use it in php variable, but can not figure out how. Would you assist me. Thank you.
Here is my code:
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
My goal is, after taking the value to make some calculation with it and show results. Thank you for the help.
This is one option:
<select name="pcs" id="pcs" onchange="this.form.submit()">
<option class="option" value="8" selected">8</option>
<option value="12" <?php if($pcs =="12") { echo "selected"; } ?>>12</option>
<option value="16" <?php if($pcs =="16") { echo "selected"; } ?>>16</option>
<option value="20" <?php if($pcs =="20") { echo "selected"; } ?>>20</option>
<option value="24" <?php if($pcs =="24") { echo "selected"; } ?>>24</option>
<option value="24+" <?php if($pcs =="24+") { echo "selected"; } ?>>над 24</option>
</select></br>
But I do not want the page to reload.
Using jQuery get the select box value on change or submit, pass the value to a php file via Ajax say calculation.php, Do the necessary calculation and echo the result in calculation.php. Get the value and display inside DIV or desired place.
EXAMPLE BELOW:
main_file.php
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pcs").on('change', function(){
var pcs_val = $(this).val();
$.ajax({
type: "POST",
url: "calculation.php",
data: { pcs: pcs_val},
success: function(theResponse) {
$('#outputDiv').html(theResponse);
}
});
});
});
</script>
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
<div id="outputDiv">--output here--</div>
calculation.php
<?php
$pcs = isset($_POST['pcs']) ? $_POST['pcs'] : '';
echo 'Calculation Result = '.$pcs;
?>
Try the following without using AJAX:
<?php
$pcs_array = array('8'=>'8','12'=>'12','16'=>'16','20'=>'20','24'=>'24','24+'=>'над 24');
$select_pcs = '';
if(isset($_POST['pcs']) && !empty(isset($_POST['pcs']))){
$select_pcs = $_POST['pcs'];
}
if(!empty($pcs_array)){
?>
<select name="pcs" id="pcs">
<?php
$option_count = 0;
foreach($pcs_array as $k=>$v){
if(!empty($select_pcs) && $select_pcs == $k){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}elseif($option_count == 0 && empty($select_pcs)){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}else{
echo '<option value="'.$k.'">'.$v.'</option>';
}
$option_count++;
}
?>
</select></br>
<?php
}
?>

Selected as Selected Database value Codeigniter

In user edit module have a dropdown in which i show options from database.
Now i select a value from dropdown and want to set it as selected.
SO next time when i edit user i can see which was the last value selected.
When i edit a user i select some value from dropdown, and then save the record back to database. Next time when i open that record i want the option i selected last time to be shown selected.
I tried this :
<tr>
<td>Base INI File</td>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
btn_edit_file($id);
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<td>
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"><?php echo $value->base_ini_filename;
if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> </option>
<?php } ?>
</select>
</td>
<td>
<?php echo btn_edit('customer/upload_ini/edit_ini_custom/'); ?>
</td>
<script type="text/javascript">
$(document).ready(function() {
$('#base_ini_id').change(function() {
var id = $("#base_ini_id").val();
var url = "/bizrtc/customer/upload_ini/edit_ini_custom/";
$("#edit_link").attr("href",url+ id);
$("#edit_link").attr("target","_blank");
});
});
</script>
</tr>
do this :
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value)
{
?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"
<?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
<?php echo $value->base_ini_filename;?></option>
<?php } ?>
</select>
Try below code:
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>" <?php if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> >
<?php echo $value->base_ini_filename; ?>
</option>
<?php } ?>
</select>
Aside from the incorrect closing tag in HTML, I also would like to note that you might want to double check your condition:
if($value->id == $value->base_ini_filename)
Seems like you are comparing very different thing
where is the user saved value ?
first you should get the user saved value into one variable
you option tag should look like below
<option id="emp" class="specialLink" value="<?php echo $value->id;?>">
<?php
if($value->id == $usersavedvalue){echo "selected='selected'";} ?> ><?php echo $value->base_ini_filename; ?> </option>

How retain text of a drop down list and select data according to the value of selection?

Have an drop down list as follows:
<script language="JavaScript" type="text/javascript">
<!--
function showSelected()
{
var selObj = document.getElementById('city');
var cityTextObj = document.getElementById('cityText');
var selIndex = selObj.selectedIndex;
cityTextObj.value = selObj.options[selIndex].text;
}
//-->
</script>
<form method="get"><font class="text5">City</font>
<select id="city" onchange="showSelected();this.form.submit();" style="width:350px">
<option value="0">Please select</option>
<option value="1">NewYork</option>
<option value="2">Canada</option>
<option value="3">Delhi</option>
<option value="4">HongKong</option>
</select>
<input type="hidden" id="cityText" name="cityText"/>
</form>
and selecting database:
<?php
$sql = "SELECT * FROM general WHERE (day2sql >= now())";
if(isset($_GET['cityText'])){
$city1 = mysql_real_escape_string($_GET['cityText']);
$sql .= " AND (gcity = '$city1')";
}
This functions are working fine. The thing is if somebody selects HongKong, the form get submits and show the contents according to HongKong. But I am not able to retain HongKong text in the drop down list.Its going to default text "Please Select"
And there are 5 links like "ALL", "Today", "Tomorrow" etc.... if somebody selects HongKong. And clicks any of these 5 links Want get data according to HongKong ALL, HongKong TODAY, HongKong TOMORROW...... Like for all cities... Is this possible?
<option value="1" <?= $_GET['cityText'] == "NewYork" ? 'selected' : '' ?> >NewYork</option>
<option value="2" <?= $_GET['cityText'] == "Canada" ? 'selected' : '' ?> >Canada</option>
<option value="3" <?= $_GET['cityText'] == "Delhi" ? 'selected' : '' ?> >Delhi</option>
<option value="4" <?= $_GET['cityText'] == "HongKong" ? 'selected' : '' ?> >HongKong</option>
EDIT:
For ALL/TODAY/TOMORROW, it would be easier if you made them a menu or radio buttons in your form. Then when the form is submitted, the PHP can perform the appropriate query.
you need to make some modification to your code
<script language="JavaScript" type="text/javascript">
<!--
function showSelected(show) //add parameter 'show' to keep value from user click
{
var selObj = document.getElementById('city');
var cityTextObj = document.getElementById('cityText');
var selIndex = selObj.selectedIndex;
cityTextObj.value = selObj.options[selIndex].text+"&show="+show; //add variable 'show' to send
}
//-->
</script>
<form id="frm" method="get"><font class="text5">City</font>
<select id="city" style="width:350px">
<option value="0">Please select</option>
<option value="1" <?php if($_GET["cityText"] == "NewYork") echo "selected"; ?> >NewYork</option>
<option value="2" <?php if($_GET["cityText"] == "Canada") echo "selected"; ?> >Canada</option>
<option value="3" <?php if($_GET["cityText"] == "Delhi") echo "selected"; ?> >Delhi</option>
<option value="4" <?php if($_GET["cityText"] == "HongKong") echo "selected"; ?> >HongKong</option>
</select>
</form>
<a href="#" onclick='showSelected("ALL");document.getElementById("frm").submit();'>ALL</a>
<a href="#" onclick='showSelected("TODAY");document.getElementById("frm").submit();'>TODAY</a>
<a href="#" onclick='showSelected("YESTERDAY");document.getElementById("frm").submit();'>YESTERDAY</a>
<a href="#" onclick='showSelected("TOMORROW");document.getElementById("frm").submit();'>TOMORROW</a>
and your php code:
<?php
$show = $_GET["show"]; //using $show you can set the query to ALL,TODAY, or etc
$sql = "SELECT * FROM general WHERE (day2sql >= now())";
if(isset($_GET['cityText'])){
$city1 = mysql_real_escape_string($_GET['cityText']);
$sql .= " AND (gcity = '$city1')";
}

Categories