Page 1 of 1
Echo php variable as a document.getElementByID() arguement
Posted: Wed Mar 28, 2007 2:39 pm
by jdhorton77
I have a registration form where the user enters their information and try for a username. If the user name is taken already I'm reloading the page with the message stating so. My problem is that I'm trying to keep the user from having to retype all their data again by using something like this:
Code: Select all
<input type="text" value="<?php if(isset($city)) {echo $city;} ?>"
This works, but I have a selection box for states, and I have to use:
Code: Select all
document.getElementById("id").selected=true
to reselect the proper option.
Where I'm coming in conflict is when I use the DOM object I'm not sure how I can echo the php $state variable for the DOM objects arguement. This is what I have so far.
Code: Select all
<?php if(isset($state)) {?><script type="text/javascript">document.getElementById(<?php if(isset($state)) {echo $state;}?>).selected=true</script> <?php ;} ?>
I've also tried having the php arguement in quotes, but no luck. Any ideas?
Posted: Wed Mar 28, 2007 3:02 pm
by feyd
You don't echo the state of the select. Instead, what you do is while iterating over the array that generates the select you compare the current value with the selected value. When they match you output
into the option element's attributes.
Re: Echo php variable as a document.getElementByID() argueme
Posted: Wed Mar 28, 2007 3:05 pm
by Begby
Code: Select all
<?php if(isset($state)) { ?>
<script type="text/javascript">document.getElementById('<?= $state ?>').selected=true;</script>
<?php } ?>
Try that
(edit: but what feyd said is the more proper solution)
Posted: Wed Mar 28, 2007 3:18 pm
by RobertGonzalez
Why are you using javascript for this at all? What I usually do is initialize all my form values to empty strings. Then, if the form is posted, I read the validated, filtered form input into those vars. If the form was not successful, all of the information in those post vars are captured with the assigned vars which are always passed to the form regardless of whether the form as ever posted or not.
Posted: Wed Mar 28, 2007 3:20 pm
by jdhorton77
I'm not sure what feyd is trying to say there. Does he mean not using php at all for this. Not sure what array he's talking about. Can you explain alittle more please. Thanks.
Posted: Wed Mar 28, 2007 3:25 pm
by RobertGonzalez
Code: Select all
<?php
$test = array(
'ab' => 'First',
'cd' => 'Second',
'ef' => 'Third',
'gh' => 'Fourth',
'ij' => 'Fifth'
);
$selected = 'ef'; // This could be 'Third' as well
echo '<select name="selectname">';
foreach ($test as $k => $v)
{
echo '<option value="' . $k . '"';
if ($k == $selected)
{
// If you were using 'Third' you'd compare against $v
echo ' selected="selected"';
}
echo '>' . $v . '</option>';
}
echo '</select>';
?>
Edit | For some reason I forgot how to write markup today. Sheesh.
Posted: Wed Mar 28, 2007 4:53 pm
by coombesy
what i do is use js history.back().
so in the page that evaluates the data i have
Code: Select all
//if user name is taken
if(username_is_taken()){
print "<script>alert('username is allready taken \n please try another')</script>";
print "<script>history.back()</script>";
}
else{
//do what ever you want with the data
}

the alert shows and the browser goes back to the form with all the original data in it!
Posted: Wed Mar 28, 2007 5:03 pm
by RobertGonzalez
That will only work under the proper cache-control settings as some pages can be made to force revalidation of the page. Seriously, it is not that hard to capture the information and pump it back into the form. Heck, you are capturing it anyway to check it. Why not use what you have and not use back?
Posted: Wed Mar 28, 2007 5:19 pm
by coombesy
a senario i never thought of Everah, you know what they say -> live and learn
