<select name="direction">
<option value="<a href='1.php'>1</a>">1</option>
<option value="<a href='2.php'>2</a>">2</option>
</select>
I'm trying to insert a href into a database but the double quotes are messing it up how do I properly use double or single quotes in this situation?
Can you try this,
<select name="direction">
<option value='<a href=1.php>1</a>'>1</option>
<option value='<a href=2.php>2</a>'>2</option>
</select>
You can not do like that. Just try like below:
Your html will be like this;
<select name="direction" onchange="onSelect()">
<option value="1">1</option>
<option value="2">2</option>
</select>
And Javascript will be like this-
function onSelect() {
if (document.getElementById("direction").value == "1"){
window.location.href = "1.php";
}
elseif (document.getElementById("direction").value == "2"){
window.location.href = "2.php";
}
}
You should escape your html before inserting it into database. You can use addslashes(string $str) to escape values which contains HTML content.
on server side
$myvalue = "<a href='1.php'>1</a>"; // value from selected option
$escaped_myvalue = addslashes($myvalue); // $myvalye becomes "<a href=/'1.php/'>1</a>"
inser_into_table($escaped_myvalue);
http://php.net/manual/en/function.addslashes.php
Related
I want to use PHP to change a div's information based on what option is selected. Below is my HTML & PHP. I don't think I'm using the right operator or syntax in the if statement. I basically want to pull information from a table in my database based on the option selected. Right now I do not have that in my code because it is irrelevant at this point. Any help is appreciated.
<select id=\"numberofstaff\">
<option class=\"staffselection\" value=\"\">--Select--</option>
<option class=\"staffselection\" value=\"smalljobsite\">1-3 staff</option>
<option class=\"staffselection\" value=\"mediumjobsite\">4-7 staff</option>
<option class=\"staffselection\" value=\"largejobsite\">8+ staff</option>
</select>
<?php
$selectOption = $_POST['numberofstaff'];
echo "<div id='jobsite_price'><p>";
if ($selectOption == 'smalljobsite'){
echo "smalljobsite";
};
if ($selectOption == 'mediumjobsite'){
echo "mediumjobsite";
};
if ($selectOption == 'largejobsite'){
echo "largejobsite";
};
echo "</p></div>";
?>
You need form tags and a POST method, while giving the select a name attribute. You can't rely on an id alone, not without JS/Ajax anyway.
Sidenote: I removed the \ from the escaped quotes, since they're not in an echo statement/PHP in your posted code. This would throw an error/warning such as "unquoted attribute". If you are using it inside an echo'd statement, then you'll need to add those back in.
FYI: Escaping quotes \" is only done when inside an echo/PHP.
Additional note:
If you want to run seperate SQL statements, it's rather simple. Where the echos are and where I have in a comment form // RUN SQL HERE, you can simply include a file in there with your SQL query. I do it myself and it cuts down on a lot of code and is more manageable that way.
HTML/PHP:
<form method="post" action="">
<select id="numberofstaff" name="numberofstaff">
<option class="staffselection" value="">--Select--</option>
<option class="staffselection" value="smalljobsite">1-3 staff</option>
<option class="staffselection" value="mediumjobsite">4-7 staff</option>
<option class="staffselection" value="largejobsite">8+ staff</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$selectOption = $_POST['numberofstaff'];
echo "<div id='jobsite_price'><p>";
if ($selectOption == 'smalljobsite'){
// RUN SQL HERE
echo "smalljobsite";
}
if ($selectOption == 'mediumjobsite'){
// RUN SQL HERE
echo "mediumjobsite";
}
if ($selectOption == 'largejobsite'){
// RUN SQL HERE
echo "largejobsite";
}
echo "</p></div>";
} // brace for if(isset($_POST['submit']))
?>
Foonotes:
To include files, you can use include():
// Include your SQL query
include('query_file.php');
where you see // RUN SQL HERE in commented code.
Another option is to use Ajax.
Here are a few links:
http://www.w3resource.com/ajax/working-with-PHP-and-MySQL.php
http://www.tutorialspoint.com/php/php_and_ajax.htm
How to use AJAX to perform a MYSQLI query (prepared statement)
If you're just using php to check the selected value, you can use jquery for that, i.e.:
$("select").on('change', function() {
$("#jobsite_price").html("<p>"+$(this).val()+"</p>")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="numberofstaff">
<option class="staffselection" value="">--Select--</option>
<option class="staffselection" value="smalljobsite">1-3 staff</option>
<option class="staffselection" value="mediumjobsite">4-7 staff</option>
<option class="staffselection" value="largejobsite">8+ staff</option>
</select>
<div id='jobsite_price'></div>
You can use pure javascript to do this.
window.addEventListener('load', function(){
var el = document.querySelector("#numberofstaff");
el.addEventListener('change', function(){
var data = { numberofstaff : el.value };
var request = new XMLHttpRequest();
request.open('POST', 'YourUrl.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
request.onload = function(result){
document.querySelector("#jobsite_price").innerHTML = result.responseText;
}
});
});
I am trying to get drop down select value posted upon send.
Markup
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
php
$selectOption = $_POST['taskOption'];
{
$message.=$value.'<br>';
}
$message.='
What am I doing wrong?
Is this what you're trying to do?
if(!empty($_POST['taskOption']))
{
$value = $_POST['taskOption'];
$message.=$value.'<br>';
}
you need to use post in your form, method="post"
if(isset($_POST['submit'])){
$dropdwon = $_POST['taskOption'];
//code here
}
I have a form where I need to select multiple items from a single dropdown and use them in a query.
I have used the tag for the dropdown and used the 'multiple' attribute to allow multiple selection.
I want to get all the selected items/indexes in a variable/array so that I can use it in tha query.
I tried using document.getElementById('statusselect').selectedindex but I get only 1 and not all.
I even tried using $('#statusselect option:selected') in jquery but in vain.
Please help me out with this!!
Thank You.
This the code for the dropdown where the values are fetched from database.
I want to get all the selected values in a variable on a button click.
echo "<td>Status<br>";
echo "<select multiple name=\"status\" size=5 id=\"statusselect\">";
foreach($status_all as $status)
{
if (isset($_GET['status']) && $_GET['status'] == $status)
{
echo "<option value=\"$status\" selected>".status_in_words($status)."</option>";
}
else
{
echo "<option value=\"$status\" >".status_in_words($status)."</option>";
}
}
echo "</select>";
echo "</td>";
jQuery :selected on multiple select must use each to iterate through all the elements. As http://api.jquery.com/selected-selector/ suggested, use this:
<select name="garden" multiple="multiple">
<option>Flowers</option>
<option selected="selected">Shrubs</option>
<option>Trees</option>
<option selected="selected">Bushes</option>
<option>Grass</option>
<option>Dirt</option>
</select>
<script>
$("select option:selected").each(function () {
// your code
});
</script>
Here is a sample HTML:
<select id="multiselect" name="multiselect[]" multiple="multiple" size="5">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Using jQuery:
$("#multiselect").val()
Yields an array with selected values
I have this HTML piece of code already read into $html. I had extracted some correct information, but I'm stuck getting the selected option value of a select.
<select name="a" id="selstart">
<option value="00">Jan</option>
<option value="01">Feb</option>
<option value="02">Mar</option>
<option value="03">Apr</option>
<option value="04">May</option>
<option value="05">Jun</option>
<option selected value="06">Jul</option>
<option value="07">Aug</option>
<option value="08">Sep</option>
<option value="09">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
and need to extract the value "06" into a variable.
I tried:
foreach($html->find('select') as $element) {
if ($element->id == 'selstart')
{
$v = $element->find('option selected',0)->value . '</br>';
}
}
and many other combinations following the idea found in php , simple_html_dom.php, get selected option but did not work.
Any ideas?
Use element[attr] to select elements with the specified attribute.
$v = $element->find('option[selected]', 0)->value . '</br>';
Thank you, for your reply, if you want to set option values after reading the select you can do like this:
First loaded the html select from another file for example,
It is something like this:
<div>
<select id="select1">
</select>
</div>
Then you can do like this:
<?php
$html=str_get_html($filename_or_method_to_the_html_content);
$products='<option></option>'.
'<option val="1">Val 1</option>'.
'<option val="2" selected="selected">Val 2</option>'.
'<option val="3">Val 3</option>';
$html->find('select[id=select1]','0')->innertext=$products;
?>
It worked well for me ;)
try this simple code
$element = $html->find('#selectIDGoesHere',0)->find('option');
foreach($element as $elemen) {
echo "Display text:".($elemen->plaintext)."<br>";
echo "value:".($elemen->value)."<br>";
}
How can I add html value for select menu options:
<select name="symbol">
<option>select</option>
<option value="<img src="start.jpg"">start</option>
<option value="<img src="end.jpg"">end</option>
</select>
I want post value to show an image.
I read this : How to add a images in select list
but I want send image value.
If you're handling the post with PHP or some similar server-side code, you should pass through only the reference to the image. Any tags can be output using the server-side code. For example:
<select name="symbol">
<option>select</option>
<option value="start.jpg">start</option>
<option value="end.jpg">end</option>
</select>
And then handle this by doing something like:
echo '<img src="'.$_POST['symbol'].'" />';
Use single quotes instead of double qoutes inside value...
<select name="symbol">
<option>select</option>
<option value="<img src='b.jpg'>">start</option>
<option value="<img src='a.jpg'>">end</option>
</select>
You have to use single quote and close tag. Try this,
<select name="symbol">
<option>select</option>
<option value="<img src='start.jpg'>">start</option>
<option value="<img src='end.jpg'>">end</option>
</select>