submit from on selcted value when page load - php

This is my php form i want to submit from on selected value on page load how it's possible. on page load i want to select Architect as a selected value.
<select name="with" <?php if(isset($_REQUEST['market']) && !empty($_REQUEST['market'])) { ?> onChange="if(this.value=='1'){window.location='?market=<?php echo $_REQUEST['market']; ?>&with=1'} else if(this.value=='2') {window.location='?market=<?php echo $_REQUEST['market']; ?>&with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}; <?php } else { ?> onChange=" if(this.value=='1'){window.location='?with=1'} else
if(this.value=='2') {window.location='?with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php"; ?>'}; <?php } ?>">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>

Try not using inline javascript. I am not the best at Javascripting but see if this works. Your description is not very clear so hopefully this is what you are looking for! The loading and so forth technically works, but whether it works like you are expecting is another story:
jQuery Libraries
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
Select Button (Add id)
<select name="with" id="with-it">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>
Script for onChange
<script>
$("#with-it").change(function() {
var ValueSet = $(this).val();
<?php if(isset($_REQUEST['market'])) { ?>
if(ValueSet == '1') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else if(ValueSet == '2') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else {
window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}
<?php }
else { ?>
if(ValueSet == '1'){
window.location='?with=1';
}
else if(ValueSet =='2') {
window.location = '?with=2';
}
else {
window.location = '<?php echo $baseUrl."viewMeetings.php"; ?>';
}
<?php } ?>
});
</script>

Related

Reload URL with param depending on value of dropdown

I would like to reload an URL with special parameters when a selection in a dropdown is made. The actual result is, that the URL changes and I see the URL with the parameter in the browserbar, but the page does not really reload/refresh.
Thats the code where I fetch the sorttype param ... in fact a reload or refresh is not done, because other code is also not processed
// parse url
$url_components = parse_url($compurl);
// get actual url path
$url = ltrim($url_components['path'], "/"); // ltrim($_SERVER['REQUEST_URI'], "/");
// get parameters from url
$sorttype = "";
parse_str($url_components['query'], $params);
$sorttype = $params['sorttype'];
Thats the code where I place the dropdown
<!-- show sortoptions in dropdown-box with redirect on selection change -->
<form action="" method="post" style="float: right;">
<strong>Sortierung:</strong>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>
</form>
There were multiple problems but the main is misunderstanding the post and get. When you want to use post, you don't change the url. You only need to reload the page with your variable set in $_POST. I have edited the code for minimal length:
<!DOCTYPE html>
<html lang='en'>
<head>
<?php
print_r($_POST);
#print it if want to see if it was set
$sorttype = $_POST['sorttype']; #assign the var
if (!isset($sorttype)){$sorttype="Not sorted";} #to overcome multiple checks
?>
</head>
<form action="" method="POST" style="float: left;">
<strong>Sortierung:</strong>
<select name="sorttype" style="margin-top:6px;" onchange="this.form.submit()">
<option value="Foo" <?php if (($sorttype == 'Foo')) { echo 'selected'; } ?>>Foo</option>
<option value="Bar" <?php if (isset($sorttype) && ($sorttype == 'Bar')) { echo 'selected'; } ?>>Bar</option>
</select>
</form>
</html>
Select name is the var name for post, selected has to be at the end before closing tag like this: selected>. onchange only needs to submit the selected value this.form.submit()
The following works as requested, no need for form tag.
The first line is for demo, how to use the parameter that is passed.
<h1>Got <?php echo isset($_GET['sorttype'])? $_GET['sorttype'] : 'no value selected yet'; ?></h1>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>

Select option value

I have a selection dropdown. Now what i want to accomplish is having a default first value (insted of an empty line in the pulldown menu).
The value needs to be "All manufacturers" and should be displayed always (like a placeholder.
If nothing is chosen, it will automatically search all the products of all manufacturers
<option <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> ></option>
<?php usort(
$manufacturers,
function($a, $b) {
return strcmp($a['name'], $b['name']);
}
)
?>
<?php foreach ($manufacturers as $manufacturer) { ?>
<?php if ($manufacturer['manufacturer_id'] == $manufacturer_id) { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>" selected="selected"><?php echo $manufacturer['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>"><?php echo $manufacturer['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
Any help appreciated!
If I understand you correctly I think you want to do this
<option value"0" <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> >All Manufacturers</option>

Interactive drop down menu that auto fills drop down values depending on what user inputs

I'm trying to create an interactive drop down menu that uses mysqli query to autofill the values in each drop down. So far the first drop down menu echo's the Brands in the database for each part so that works fine. However the second drop down displays regardless of the $selected value being true or false, but also when I do select a brand it doesn't auto fill the results.
<?php
foreach($parts as $col) { ?>
<dl>
<dt><?php echo $col?></dt>
<dd>
<select id="search_status" name="state" data-placeholder="State">
<?php
$brand = $database->fetch_col($col);
$selected = false;
?>
<option value=""></option>
<?php foreach($brand as $b){ ?>
<option value="<?php $selected = true; ?>"><?php echo $b['Brand']; ?></option>
<?php }?>
</select>
<?php if($selected = true){
$item = $database->fetch_row($col, $b);
?>
<select>
<option></option>.
<?php foreach($item as $i) {?>
<option><?php echo $i['Model']; ?></option>
<?php }?>
</select>
<?php
} else {
echo "Please select Brand";
}
?>
<p><?php
echo var_dump($item);
?><p>
</dd>
</dl>
<?php }?>
I'm fairly certain its the $selected variable not corresponding with the rest of the form.
You are assigning the $selected to true by using a single equal sign - if you want to compare the value to true you need to use == or ===.
http://php.net/manual/en/language.operators.comparison.php
You can use ajax to do it.Here i share some example.YOu can find your solution from it...
Main page:
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("POST", "ajax.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
AJax Page
ajax.php
<?php
echo "ss";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST">
Sign up
Sign up
Sign up
Sign up
</form>
</body>
</html>
Hope it will help you.

HTML Option tag with selected value from PHP

Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>

php - After validation why my select tag data get cleared

I am doing PHP validations for my html values. However when PHP validation fails and I return back to the page, the select tag form data is cleared. Is there anyway to do save and reload the form data in php
<?php
$qualific=$passingyear="";
$qualificErr=$passingyearErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
//qualification validations starts here
if(empty($_POST["qualif"]))
{
$qualificErr="* Qualification is Required";
$valid=false;
}
else
{
$qualific=test_input($_POST["qualif"]);
}
//qualification validations starts here
/*yearOfPassing validation starts here*/
if(empty($_POST["yearpass"]))
{
$passingyearErr="* Year Of Pass is Required";
$valid=false;
}
else
{
$passingyear=test_input($_POST["yearpass"]);
}
/*yearOfPassing validation starts here*/
//if valid then redirect
if($valid){
include 'database.php';
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';
exit;
}
}
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Qualification<span class="error">*</span>:</label>
<select name="qualif">
<option label="Select"></option>
<option>Below SSC(10 Std)</option>
<option>SSC(10 Std) passed</option>
<option>HSC(12 Std) passed</option>
<option>Graduate</option>
<option>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option>1975</option>
<option>1976</option>
<option>1977</option>
</select>
</form>
try <?php if($_POST["qualif"] == "<value>") echo "selected"; ?> in each option tag.
like
<option <?php if($_POST["qualif"] == "Below SSC(10 Std)") echo "selected"; ?>>Below SSC(10 Std)</option>
Currently you are using <option> without values.
Use like :
<option value="SSC" <?php if(isset($_POST['qualif']) && $_POST['qualif'] == 'SSC') { echo "selected"; } ?> >SSC</option>
your are missing value parameter inside <option>.
you need to add some code inside <option> tag
Please try following code hope this will solve the issue.
<select name="qualif">
<option label="Select"></option>
<option value="Below SSC(10 Std)" <?php if($qualific == 'Below SSC(10 Std)') {?> selected <?php } ?>>Below SSC(10 Std)</option>
<option value=">SSC(10 Std) passed" <?php if($qualific == '>SSC(10 Std) passed') {?> selected <?php } ?>>SSC(10 Std) passed</option>
<option value="HSC(12 Std) passed" <?php if($qualific == 'HSC(12 Std) passed') {?> selected <?php } ?>>HSC(12 Std) passed</option>
<option value="Graduate" <?php if($qualific == 'Graduate') {?> selected <?php } ?>>Graduate</option>
<option value="Post Graduate" <?php if($qualific == 'Post Graduate') {?> selected <?php } ?>>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option value="1975" <?php if($passingyear == '1975') {?> selected <?php } ?>>1975</option>
<option value="1976" <?php if($passingyear == '1976') {?> selected <?php } ?>>1976</option>
<option value="1977" <?php if($passingyear == '1977') {?> selected <?php } ?>>1977</option>
</select>
You should use jquery
<script type="text/javascript">
$(document).ready(function()
{
$("#id").val("<?php echo $_POST['qualif']; ?>");
});
</script>

Categories