retain select option value in php - php

<select class="form-control" id="oid">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<?php
echo $row['id'];
?>
i want to echo this select option value in php , it will catch easily in javascript but cant echo in php

it will catch easily in javascript but cant echo in php
PHP is a server-side scripting language, which means that in your case you could only echo the option value of the selection after submitting it to the server. Here echo $row['id']; will just print the last id fetched from your database.
(Client-side) JS executes in the browser, this allows you to track user selections as they are happening.
<form action="#" method="post">
<select class="form-control" id="oid" name="userselection">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<input type="submit" name="submit" value="Submit selection" />
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['userselection'];
}
?>

Related

How to change form submission into SEO friendly URL

I have a simple form that compares two products using a select form and when user hits submit, it redirects to another site but the problem is the URL is rather long and ugly looking right now its displayed like this:
product_details.php?item1=core-i3-7300&submit=Compare&item2=core-i5-12400
I would like the form to submit in this URL structure:
product/item1/item2
Here is the code for my form, I also already have the HTACCESS rule created to accept the URL structure I want, I just need to correct format from the submission form.
<form action="compare/" method="GET" onSubmit="return validateThisFrom (this);" >
<select id="selectPlatform" Name="item1" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
<button input type="submit" name="submit" value="Compare" class="button red">
<select id="selectPlatform2" Name="item2" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
</form>

php dynamic dropdown menu get value

i am trying to do a dynamic dropdown menu, i manage to retrieve the first menu value but i can't manage to retrieve the second menu value.
HTML part
<div>
<label for="marca">Marca </label>
<select type="text" id="marca" name="marca" onChange="getModel()">
<option value="">Alege Marca</option>
<?php while($row = mysqli_fetch_assoc($resultMarca)){ ?>
<option value="<?php echo $row["id"] ?>"> <?php echo $row["nume_marca"] ?> </option>
<?php } ?>
</select>
</div>
<div id="model_masina">
<label for="model">Model </label>
<select id="model" nume="model">
<option value="">Alege Model</option>
</select>
</div>
Ajax Part
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type ="text/javascript">
function getModel(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","get_model.php?marca="+document.getElementById("marca").value, false);
xmlhttp.send(null);
document.getElementById("model_masina").innerHTML=xmlhttp.responseText;
}
function model_schimba(){
$modelSc = (document.getElementById("model").value);
}
</script>
PHP
?>
<label for="model">Model </label>
<select id="model" nume="model" onchange='model_schimba()'>
<option value="">Alege Model</option>
<?php
while($row = mysqli_fetch_array($res)){ ?>
<option value="<?php echo $row["id"] ?>"> <?php echo $row["name"] ?> </option>
<?php }
?> </select> <?php
}
i mange to take the variable here
$modelSc = (document.getElementById("model").value);
but when i push the submit button i can't reach the variable
$model = $_POST["model"];
"but when i push the submit button i can't reach the variable $model = $_POST["model"];"
nume="model"
PHP syntax is English-based, not in your language.
You need to change it to name="model".
The "name" attribute is the same in any language.
Having use PHP's error reporting, it would have thrown you an undefined index notice.
http://php.net/manual/en/function.error-reporting.php
First, code separation is important for readability.
Second, I think your AJAX return
document.getElementById("model_masina").innerHTML=xmlhttp.responseText;
is mistakenly pointing at a <div> container instead of the <select> list. Should be,
document.getElementById("model").innerHTML=xmlhttp.responseText;
because you are outputting select menu <option>

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'];
}

make an option from select tag stays the same after reload or change the page, PHP

So im having problems on figuring out how to make option tag so when is pressed it will make the action on all the pages
This is what i have done:
index1.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
index2.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
select.php
<div id="regContainer">
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option value="miami">miami</option>
<option value="new york">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
</div>
process.php
<?php if(!isset($_SESSION)){ session_start();}
if(isset($_POST['selectedRegion'])){
$region = $_POST['selectedRegion'];
if($region == "miami"){
echo "miami selected";
$title = "miami";
}
if($region == "new york"){
echo "new york selected";
$title = "new york";
}
}
?>
when I press the first option, it changes the title normally on the index1.php, but when i go to the index2.php... it does not change the title of that page, I have to press the first option button again to make the change.
Is there anyway to just press the first option from the selection and then change the title of all the pages without having to press the button again?
Any suggestion will help. Thanks
Using the code you currently have you could do the following
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_REQUEST['selectedRegion'] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_REQUEST['selectedRegion'] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
This will ensure the option is selected on page refresh. If you want something that also acts on page change then you will need to use a cookie
if(isset($_REQUEST['selectedRegion'])){
setcookie("region", $_REQUEST['selectedRegion'], time()+3600);
}
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_COOKIE["region"] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_COOKIE["region"] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
You need to change
if(!isset($_SESSION)){ session_start();}
To just
session_start();
What you're saying currently is if you have a session set, don't load the session library to then use that session data.

How to hyperlink datalist elements in php?

I am new in php, and trying to use datalist on my project. But I don't know how to use hyperlink in option values used in a datalist.
Here is the code:
<input type="text" list="gt" />
<datalist name="dt" id="gt" >
<?php
while($result33=mysqli_fetch_assoc($select33))
{
?>
<option value="artistdetail.php?id=<?php echo $result33['ID']; ?>"> <?php echo $result33['name']; ?> </option>
<?php
}
?>
</datalist>

Categories