Once it loads in my page, if nothing has been saved in the DB table, all options are shown. As soon as i make a selection and reload the page, the selected option dissapears from the list and isn`t reloaded in the dropdown. Instead, it displays the next value which takes the place of the selected one.
if i check the SQL statement and the $str, it does load all the options except the one which is selected which is in $getBris (it has a value).
What could be causing my select to not display my selected option and instead removing it from the list?
*It specifically doesnt work in IE8, wasnt working in Firefox but now it does
<script src="validation.js" type="text/javascript"></script>
<html>
<body onLoad="checkSecondValue();">
</body>
</html>
<?php
//retrieve all the bris for the drop down
include '../../inc/database.php';
// ORDER BY RAND()
$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";
$getBris = $_GET['bris'];
$getBris = $getBris - 1;
print_r("bris is : "+ $getBris);
if($getBris == null)
{
$str .= "<option value='' selected></option>";
}
else
{
$str .= "<option value='999'>Choisir un bris</option>";
}
$i = 0;
while($data = mysql_fetch_assoc($res))
{
if($data['F_BRISID'] == $getBris)
{
$str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
}
else
{
$str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
}
$i++;
}
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
$str .= "</select>";
echo $str;
if(is_numeric($bris))
{
echo "<script type=\"text/javascript\">alert('test');checkSecondValue();</script>";
}
?>
Use your browser's View Source feature to inspect the actual HTML you are generating (which is, in fact, the only see the browser ever sees). It looks like you're inserting random single quotes.
Update:
<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>"
... will render as:
<option value='blah' selected '>blah</option>
It's the only error I've cared to spot but an HTML validator should find them all. Also, I recommend you use this syntax:
<option value="blah" selected="selected">blah</option>
A construct like this
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
is highly wasteful of space and forces you to duplicate a big chunk of html whose only difference is the "selected" attribute. Why not do something like this:
$selected = ($getBris == 12) ? ' selected' : '';
$str .= "<option value=12{$selected}>Autre</option>";
Related
I am trying to use jQuery to change the background color of the select element based on the option chosen within.
To generate the select elements, I'm using the following code:
while ($qrow = $qquery->fetch(PDO::FETCH_ASSOC)) {
$aqanswer = "qAnswer" . $qrow['questionID'] . "";
$aqcomments = "qComments" . $qrow['questionID'] . "";
echo "<tr><td class='auditf'>" . $qrow['qDesc'] . "</td><td class='auditm'><select name='qAnswer" . $qrow['questionID'] . "' form='entryform' id = 'selectqAnswer". $qrow['questionID'] ."'>";
if (isset($_SESSION[$aqanswer])) {
if ($_SESSION[$aqanswer] === 'Green') {
echo "<option value='Green' selected='true' class = 'green'>Green</option>";
echo "<option value='Red' class='red'>Red</option>";
} elseif ($_SESSION[$aqanswer] === 'Red') {
echo "<option value='Green' class ='green'>Green</option>";
echo "<option value='Red' selected='true' class = 'red'>Red</option>";
}
unset($_SESSION[$aqanswer]);
} else {
echo "<option value='Green' selected='true' class = 'green'>Green</option>";
echo "<option value='Red' class = 'red'>Red</option>";
}
echo "</select></td><td class='auditl'><textarea name='qComments" . $qrow['questionID'] . "' rows='3' cols='30' maxlength='255' value='N/A'>";
if (isset($_SESSION[$aqcomments])) {
echo $_SESSION[$aqcomments];
unset($_SESSION[$aqcomments]);
} else {
}
echo "</textarea></td></tr>";
}
And my jQuery:
$("[id^=selectqAnswer]").change(function () {
var color = $("option:selected", this).attr("class");
$("[id^=selectqAnswer]").attr("class", color);
});
Now this does work, but it causes every single box on the page to change, obviously. The problem is, the number of boxes on the page can change based on user options, so I don't have a fixed list of select IDs that I could reference individually in my jQuery. I've tried Googling the issue, but every single result talks about specifically naming the ID, not working with an ID that's generated procedurally. How do I only change a specific select element's background color based on the select option selected when I don't know what the ID will specifically be ahead of time.
$("[id^=selectqAnswer]").attr("class", color);
this will change the color for every #selectqAnswer select, if you want to change the color of the current select use this
Javascript
$(this).attr("class", color);
Following is an ajax post page which renders the checkboxes on run-time. I am facing issue while writting the script for select all button, when I click on the button only 1 value is getting selected not the entire array:
<?php
session_start();
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("../includes/functions.php");
if(isset($_REQUEST['t']))
{
$td = $_REQUEST['t'];
$t = split(",",$td);
$all = "";
$box_in_row = 0 ;
$this_box="<table border=0><tr>";
foreach($t as $table)
{
$this_box = "<td><h3>$table</h3>";
$result = mysql_query("SHOW FULL COLUMNS FROM $table FROM prfxcom1_prfx");
$options = "";
while($r = mysql_fetch_object($result))
{
if(!empty($r->Comment))
{
$options .= "<br><input type=checkbox name=\"".$table."[]\" value='$r->Field' id=\"$table\">" . $r->Field;
}
}
if($table == "transfer_req")
{
$options .= "<br><input type=checkbox name=\"".$table."[]\" value='Net Profit' id=\"$table\">NetProfit";
}
$this_box .= $options;
// Button
$click = "$('#$table').attr('checked', 'checked')";
$button = "<br /><input style='margin-top:10px;' type='button' name='$table_button' id='$table_button' value=' Select All ' onclick=\"$click\"/>";
$all .= "<div class='tblBox'>".$this_box.$button."</div></td>";
}
//$all = "<table class=\"listing form\" cellpadding=\"0\" cellspacing=\"0\">".$all."</table>";
echo $all;
}
?>
Issue is faced in the line:
$click = "$('#$table').attr('checked', 'checked')";
Please suggest, I am stuck on this.
Thanks,
Hardik
WHAT???
$click = "$('#$table').attr('checked', 'checked')";
How can you write Javascript in the middle of a PHP file? It needs to be in script tags but even then PHP runs at the server and will not render your Javascript for you.
Add script tags, change your ID's to separate ones and give them the same class like tableClassName, and then write the following.
$(function(){
$('.tableClassName').attr('checked', 'checked')";
});
Ignoring the many issues with the code and simply answering the question:
You need to refer to the checkboxes using a class name not a ID (you have given them all the same ID)
For these lines: $options .= "<br><input type=checkbox name=\"".$table."[]\" value='$r->Field' id=\"$table\">" . $r->Field;
Change to: $options .= "<br><input type=checkbox name='" . $table . "[]' value='" . $r->Field ."' class='" . $table . "'>" . $r->Field;
For this line: $click = "$('#$table').attr('checked', 'checked')"; use single quotes or escape the $
Change to: $click = '$("."'.$table.'").attr("checked", "checked")';
I am pulling out record from the database and inserting them inside a dropdown like this:
echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";
It works but I need to be able to click on an option on the dropdown and make it link just like:
Record1Here
Record2Here
Record3Here
UPDATE: Latest code:
<script>
function doSomething() {
var currentval = this.options[this.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
</script>
...
echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option value=\"view.php\">" . $drow['id'] . "</option>";
}
echo "</select>";
You can't place anchors on an option within a select list.
What you can do is use JavaScript and then do something on the change event of the select list :
echo "<select onchange='doSomething(this);>';
then in JavaScript do something based on the selected value :
function doSomething(elem) {
var currentval = elem.options[elem.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
Example here
you could update your PHP code to include a value in each option :
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";
I know it`s possible to call javascript functions in php and it has worked but when i try to call checkSecondValue, nothing happens. I have included my variations on calling the function.
Edit: If i try to call it at the bottom without activating the onChange event in the select, it won`t work.
<!--<html>
<head>
<script type="text/javascript" src="validation.js"></script>
</head>
</html>
<script src="validation.js" type="text/javascript"></script>
-->
<?php
//retrieve all the bris for the drop down
include '../../inc/database.php';
$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";
$bris = ($_GET['bris']);
if($bris == 4)
{
$bris2 = "autre";
}
if($bris == null)
{
$str .= "<option value='' selected></option>";
}
else
{
$str .= "<option value=''></option>";
}
$i = 0;
while($data = mysql_fetch_assoc($res))
{
if($data['F_BRISID'] == $bris)
{
$str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
}
else
{
$str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
}
}
if($bris2 == "autre")
{
$str .= "<option value='autre' selected>Autre</option>";
}
else
{
$str .= "<option value='autre'>Autre</option>";
}
$str .= "</select>";
echo $str;
if(is_numeric($bris))
{
/* echo "<SCRIPT language=\"JavaScript\" SRC=\"validation.js\">checkSecondValue();</SCRIPT>"; */
/* echo "<script> checkSecondValue();</script>"; */
/* echo "<script type=\"text/javascript\">checkSecondValue();</script>"; */
/* echo '<script type="text/javascript">', 'checkSecondValue();', '</script>'; */
/* echo "<SCRIPT LANGUAGE='javascript'>checkSecondValue();</SCRIPT>"; */
}
?>
You can't call client-side javascript functions from PHP, but you can echo code that makes the client call the function on a specific (client-side) event.
Just look at the generated HTML you get from your PHP, and from there, try to see why checkSecondValue() isn't executed on an onChange event.
At the very least, you want your function call to happen only after the page is loaded. This can be done by embedding the function in the body tag during onload:
<body onload="checkSecondValue();">
The function call is going to execute on the client-side, after the entire page has been processed by PHP. You can't communicate back-and-forth between JavaScript and PHP during the process of the page.
I'm using a form where the user can edit an entry. Everything is populating and all is well with the exception that I can't get the drop down to show the project that they've already assigned the image to.
$project_qry = "SELECT * from projects ORDER BY title ASC";
$project_res = mysql_query($project_qry);
$project_drop = "<select name=\"project_id\">\n";
while ($row = mysql_fetch_array($project_res))
{
if ($project_id == $row[title])
{
$project_drop .= "<option value=\"$row[id]\" selected>$row[title]</option>\n";
}
else
{
$project_drop .= "<option value=\"$row[id]\">$row[title]</option>\n";
}
}
$project_drop .= "</select>\n";
I'm sure it's something devilishly simple but I'm stumped.
{
if ($project_id == $row[id])
{
$project_drop .= "<option value=\"$row[id]\" selected=\"selected\">$row[title]</option>\n";
}
else
{
$project_drop .= "<option value=\"$row[id]\">$row[title]</option>\n";
}
}
You need to compare the value and not the title. It is the value that gets posted ($_POST)
selected="selected" makes it XHTML compliant.
bigstylee answered correctly. I also recommend to separate the values of the array and your string:
$project_drop .= "<option value='". $row['id'] ."'>".$row['title']."</option>";
Also drop \n. Outputting \n won't generate a line break in the browser. And it is unnecessary.