Bowlage Posted October 27, 2023 Share Posted October 27, 2023 (edited) Hi All, I'm a beginner to PHP and am having trouble with a Dropdown using PHP and SQL. This is what I have: $result = mysql_query($sql); echo " <form id='myForm' action='https://mywebsite.com/forum/index.php/'> "; echo ' <select name="/"> '; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['field_181'] ."'>" . $row['field_181'] ." - " . $row['COUNT(field_181)'] . " Events </option>"; } echo " </select> "; echo " <br><br> "; echo " <input type='submit'> "; echo '<input type="hidden" name="-bowling-tournaments" > '; echo " </form> "; So this code is working in that it's properly displaying the Dropdown items in my database. The problem is that when I click submit, it's bringing me to this URL: https://mywebsite.com/forum/index.php?/=New%2BYork&-bowling-tournaments= The URL it SHOULD be bringing me to is https://mywebsite.com/forum/index.php?/new-york-bowling-tournaments/ But I don't know how to fix it. Can anyone please assist? Edited October 27, 2023 by Bowlage Quote Link to comment Share on other sites More sharing options...
kicken Posted October 28, 2023 Share Posted October 28, 2023 1 hour ago, Bowlage said: The URL it SHOULD be bringing me to is https://mywebsite.com/forum/index.php?/new-york-bowling-tournaments Why do you think that? That is not how forms work. Form data is submitted in name=value pairs, each pair separated by an &. What you're getting is exactly what you should be getting. If you want the other URL format, then you need to either Write JavaScript code to read the form and redirect to the desired URL or Accept the standard format form data with your PHP code, and issue a redirect based on the submitted data. Quote Link to comment Share on other sites More sharing options...
Bowlage Posted October 30, 2023 Author Share Posted October 30, 2023 On 10/27/2023 at 8:58 PM, kicken said: Why do you think that? That is not how forms work. Form data is submitted in name=value pairs, each pair separated by an &. What you're getting is exactly what you should be getting. If you want the other URL format, then you need to either Write JavaScript code to read the form and redirect to the desired URL or Accept the standard format form data with your PHP code, and issue a redirect based on the submitted data. Thanks, but as I said, I'm a beginner. How would I correctly issue a redirect using PHP based on the submitted data? Quote Link to comment Share on other sites More sharing options...
kicken Posted October 30, 2023 Share Posted October 30, 2023 5 hours ago, Bowlage said: How would I correctly issue a redirect using PHP based on the submitted data? Accept the incoming data using the appropriate superglobal variables, then use the header function to output a Location header to the desired URL. Your current form is formatted well for this, so first you'd want to change it to give your select box a meaningful name, and remove your hidden input as it is not necessary. $result = mysql_query($sql); echo " <form id='myForm' action='https://mywebsite.com/forum/index.php'> "; echo '<select name="city">'; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['field_181'] ."'>" . $row['field_181'] ." - " . $row['COUNT(field_181)'] . " Events </option>"; } echo " </select> "; echo " <br><br> "; echo " <input type='submit'> "; echo " </form> "; Then in your index.php file, you need code to detect if the city input was submitted and if so, issue the redirect with header. if (isset($_GET['city'])){ $url = '/forum/index.php?/'.urlencode($_GET['city']).'-bowling-tournaments/'; header('Location: '.$url); exit; } I don't see much point in doing this though. If you want pretty URLs you should use URL rewriting or a Router library. Otherwise, just use normal query parameters/form posts. Pretty URLs are not necessary, they are just a nice touch. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.