Jump to content

PHP Input Type With Dropdown


Bowlage

Recommended Posts

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 by Bowlage
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.