Reputation: 203
Hoping somebody can help me with this. For some reason, the following form code always gives the 'route' radio button value as being 'm', even when the the option with value 's' is selected.
echo "<b><u>Route Information</u></b><br><br>";
echo "Select Route Name: <input type='radio' name='route' id='route1' value='s' onclick='switchRoute();' CHECKED><br>";
echo "<div id='txtLocation'><select name='route_name' id='route_name'>
<option value='0000'>... select a location ...</option>";
echo "</select></div><br><br>";
echo "Or enter a new route (select the radio button for this option):<input type='radio' name='route' id='route2' value='m' onclick='switchRoute();'><br>";
echo "    Route Name:<br>    <input name='new_route_name' size='50' disabled='true' class='input-xlarge'><br><br>";
echo "    Route Grade:<br>    <input type='number' name='route_grade' class='input-mini' disabled='true'><br><br>";
echo "    Route Description <i>(optional)</i>:<br>    <textarea name='route_description' class='input-xxlarge' rows='3' disabled='true'></textarea><br><br>";
The route_name select is filled out with the follwing ajax code:
function showUser(str)
{
if (str=="")
{
document.getElementById("txtLocation").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var output = '';
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
output = xmlhttp.responseText;
document.getElementById("txtLocation").innerHTML=output;
}
}
xmlhttp.open("GET","../model/selected_loc.php?l="+str,true);
xmlhttp.send();
}
Which is called by this PHP code:
echo "<b><u>Location Information</u></b><br><br>";
echo "<select name='loc_id' id='loc_id' onchange='showUser(this.value)'><option value='0000'>... select a location ...</option>";
$result = mysql_query("SELECT loc_id, name FROM location ORDER BY name");
while ($row = mysql_fetch_array($result))
{
$id = $row['loc_id'];
$name = $row['name'];
echo "<option value='{$id}'>{$name}</option>";
}
echo "</select><br><br>";
There is also this javascript code acting on the form.
function switchRoute()
{
if (document.getElementById('route2').checked)
{
document.submitroute.route_name.disabled=true;
document.submitroute.new_route_name.disabled=false;
document.submitroute.route_grade.disabled=false;
document.submitroute.route_description.disabled=false;
}
else if (document.getElementById('route1').checked)
{
document.submitroute.route_name.disabled=false;
document.submitroute.new_route_name.disabled=true;
document.submitroute.route_grade.disabled=true;
document.submitroute.route_description.disabled=true;
}
}
Thanks for any assistance you can give me.
Upvotes: 0
Views: 571
Reputation: 203
I feel rather silly, however I have solved it myself.
In the page which received the data I had:
if ($route = "m")
Instead of:
if ($route === "m")
This was reassigning the $route variable instead of comparing it. I swear I checked that earlier, however small things like that are sometimes easily missed.
Thanks to all those who helped or looked at my question.
Upvotes: 3
Reputation: 13089
Hard to say with the code you've given. There's no <form> tags in the code you've posted, so I assume they wrap the first code block.
The following bit of code when attached to a button displays the currently selected option. Can't help but wonder how you're getting the selected option and just as importantly, what you're doing with it.
JS:
function showCurrent(inputName)
{
var inputList = document.getElementsByTagName('input');
var numRadios = 0, i, n = inputList.length;
var value = null;
for (i=0; i<n; i++)
{
if ( (inputList[i].getAttribute('type') == 'radio') && (inputList[i].getAttribute('name') == inputName) )
{
numRadios++;
if (inputList[i].checked)
value = inputList[i].getAttribute('value');
}
}
alert("Num radios in the '"+ inputName + "' group: " + numRadios + ", value=" + value);
}
HTML:
<input type='button' value='getCurValue' onclick='showCurrent("route");'/>
FWIW: Passing some things to php on form submission requires a trick with the naming of the elements in the html code. I.e the name in the html has to include the array access operators []. the <option> element for example, needs to be named in this way. I've had no cause to pass radio button values this way, so dunno if they work the same - or more particularly, if this has any relevance to your question.
An example:
<form action='testCombo.php'>
<select name='test[]' multiple='multiple'>
<option value='1'>a</option>
<option value='2'>b</option>
<option value='3'>c</option>
<option value='4'>d</option>
</select>
<input type='submit' value='submit'/>
</form>
Upvotes: 0