Echo php variable as a document.getElementByID() arguement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Echo php variable as a document.getElementByID() arguement

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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

Code: Select all

selected="selected"
into the option element's attributes.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Re: Echo php variable as a document.getElementByID() argueme

Post 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)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
coombesy
Forum Newbie
Posts: 7
Joined: Wed Mar 28, 2007 3:31 pm
Location: Dublin

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
coombesy
Forum Newbie
Posts: 7
Joined: Wed Mar 28, 2007 3:31 pm
Location: Dublin

Post by coombesy »

a senario i never thought of Everah, you know what they say -> live and learn ;)
Post Reply