How to hyperlink datalist elements in php? - 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>

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>

How to disable input in datalist?

I want to block the user to input I just want him to choose only from the list
I tried disable and readonly but this will disable my list and I don't want that.
This is my code
<div class="form-group row justify-content-center" id=" input-champs " style="margin-left:;" ><!-- grossistee -->
<label for="grossiste" class="col-2 col-form-label"></label>
<div class="col-3">
<input type="text" list="list-gro" placeholder="Grossiste1" id="g_name1" name="g_name1" class="form-control">
<datalist id="list-gro">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</datalist>
</div>
From the living standard spec of the datalist element:
Each option element that is a descendant of the datalist element, that is not disabled, and whose value is a string that isn't the empty string, represents a suggestion. Each suggestion has a value and a label.
Conversely, this means that a user can enter anything he wants. The datalist element only contains suggestions that the user can use, but does not have to.
For your puprose I 'd suggest a select element. A select element dictates strict values for the user to use.
<label for="my-select">My Select</label>
<select name="my-select" id="my-select" required>
<option value="" disabled selected>Select your option</option>
<?php foreach ($databaseresult as $row): ?>
<option value="<?= htmlspecialchars($row['value']) ?>">
<?= htmlspecialchars($row['label']) ?>
</option>
<?php endforeach ?>
</select>
Its better to use select in place of input as you dont want the user to type in anything from himself/herself.
You can do like this to implement select with database query
<select name="g_name1" id="g_name1" class="form-control">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</select>

retain select option value in 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'];
}
?>

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>

Set_select() not working on CodeIgniter

I have a select box in my form and when I hit submit the form goes through validation and if an error happens, return all the value. This work expect for my select box, I have use the set_select but it doesnt work data comes in data base
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option value="" >Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
?>
<option value="<?php echo $v_category->leave_category_id ?>" >
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
The set_select() function prints out the selected="selected" attribute in the <option> element where appropriate. But your code is as follows:
<option value="<?php echo $v_category->leave_category_id ?>" >
^
closing the tag --> |
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?>
</option>
So when you look at the html, it will look something like this:
<option value="123">selected="selected" Category Name</option>
Got it? Your selected attribute is placed outside the tag. I guess you'll be able to fix it now ;)

Categories