Hi Guys

I wonder if you could help me?

I want to incorporate a drop down listbox into an existing webpage header.

This is the listbox form code I have built into the existing header template (header.tmp.php) which I think is ok :

<form action="loc.php" method="post" >
<input type="hidden" name="option" value="loc_search">
<td><select name="user_loc" onchange="this.form.submit();">
<option value="500" <? echo ($user_details['user_loc']=='A') ? 'selected' : ''; ?>><?=MSG_LOC_A;?></option>
<option value="50" <? echo ($user_details['user_loc']=='B') ? 'selected' : ''; ?>><?=MSG_LOC_B;?></option>
<option value="75" <? echo ($user_details['user_loc']=='C') ? 'selected' : ''; ?>><?=MSG_LOC_C;?></option>
<option value="100" <? echo ($user_details['user_loc']=='D') ? 'selected' : ''; ?>><?=MSG_LOC_D;?></option>
</select></td>
</form>

The listbox should post the users location to the database and hold that location until the user decides he wants to change it. If the above code is ok for the header form I am stuck with the code needed to process the information collected (loc.php)

My current code for loc.php is as follows but I suspect this is wrong.

$selected_loc = $_POST['user_loc'];
$update_sql_loc= $db->query("UPDATE " . DB_PREFIX . "users SET user_loc ='" . $selected_loc . "'WHERE user_id='" . $user_id . "'");

$get_loc = $db->get_sql_row("SELECT user_loc FROM " . DB_PREFIX . "users WHERE user_id='" . $user_id . "'");

$template->set('user_loc', $get_loc['user_loc']);

echo $template_output;

Just to confirm, the listbox form and loc.php basically need to collect the posted location from the header.tmp.php, send it to the database and then go back to the webpage with the new user location set and stored in the listbox as the new selection. Nothing else.

Any help would be very much appreciated.

Thanks in advance.

EJ

    I noticed that you are not being very careful at screening the user input before sticking it into your queries. Consider using [man]mysql_real_escape_string[/man] on any values before sticking them in a query. Or perhaps your $db object has an 'escape' function.

    Aside from that, I didn't notice any real problem with your code. Does it work?

      Hi Sneaky

      And many thanks for your reply.

      No the code does not work. Firstly, the listbox value is not written to the database, secondly, the listbox does not hold the variable as the selected value and finally, the webpage reloads to www.mydomain.com/loc.php not the page the user was originally on when he made a location selection. 😕

        Unfortunately, your code is attempting to use some classes and I don't know what the code inside these classes is doing. There's a $db object which doesn't appear to be working nor does it appear to reporting any errors. There's a $template object that is doing something and then you echo some variable $template_output that has never been defined in the code that you've shown me. Do you mean $template->output instead? Also, none of this code redirects to another page. Your problem could be in any number of places. I'd start looking at the database object. It should be reporting errors if it cannot connect to the database or if queries fail.

          Thanks Sneaky

          I have resolved the db issue and the correct value is now being saved to the database, i needed to add a session value command to pass the user_id. I have also solved the refresh issue.

          The only problem which I have now is the listbox is not holding the user's selected value, it is reverting back to the value at the top of the list. Do you know how I can fix this?

          With this type of programme the master document (abc.php - generic example) normally processes the underlying template (which would normally be abc.tmp.php) but in this case, the loc.php form is linked to an existing template (header.tmp.php) and I do not want to process the whole header template.

          So I just need to know how to force the listbox to display the users selected location by using the loc.php code.

          Regards EJ

            I see that your form submits to 'loc.php'. Given that your method is POST, you should probably have loc.php redirect to some other page. If you don't, then when people refresh their browser, they might be prompted and asked if they want to re-submit their data. Redirecting is possible using a meta refresh tag.

            If you don't want to redirect, you'll need loc.php to put the user's location in $user_details['user_loc'] because that's where the code is looking for it when it tries to initialize the select input.

              Write a Reply...