Jump to content

[SOLVED] Help with form action-submit button


ecjughead

Recommended Posts

I used a form to create a single select drop-down list which is populated by php/mysql query.  The form action directs to a certain url but I want to add "?type="xxx" to the end of the url based on the selection made.  Is this possible or is there another way to accomplish this?

 

I have been searching the net all morning for a solution...ideas?

Link to comment
Share on other sites

you can create the form selections using variables. ie select the dropdown names/values from you database , place them in an array and print them as part of the HTML.

 

Adding the ?type=xxx to the end is beyond me but I guess you could do it either by reloading the page or using javascript.

 

What exactly do you want to achieve? What do you need the?type=xx at the end of the form action url?

 

If your trying to pass a piece of information relating to the selected item to the form action script you could just test for which value was submitted and run the appropriate code.

Link to comment
Share on other sites

Use javascript with an onChange or onBlur event. Something like this:

 

<html>
<head>
<SCRIPT LANGUAGE="javascript">
<!--
function OnChange(dropdown)
{
    var myindex  = dropdown.selectedIndex
    var SelValue = dropdown.options[myindex].value
document.myform.otherbox.value = SelValue
    return true;
}
//-->

</SCRIPT>
</head>
</body>
<form name="myform" action="test.html" method="get">
<select name=select1 onchange='OnChange(document.myform.select1.value());'>
<option value=""></option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>

<input type="hidden" value="" name="otherbox">
<input type="submit">
</form>
</body>
</html>

 

 

You can use both the POST and GET with a form:

 

print "<form action=\"mypage.php?x=$myvalue\" method=\"post\">"

Link to comment
Share on other sites

Here's some js that actually works:

 

<html>
<head>
</head>
</body>
<form name="myform" action="test.html" method="get">

  
    <p><input type="hidden" name="mytext1" size="40"><br>
  <select size="1" onchange="if(this.selectedIndex > 0)
   this.form.mytext1.value=this.options[this.selectedIndex].value;
   return true;">
    <option value="">[select one]</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
    <option value="6">Six</option>
  </select></p>


<input type="submit">
</form>
</body>
</html>

Link to comment
Share on other sites

Thanks for the javascript.

 

To clarify, my current page works but I am not using a form.  I was simply using HTML to list the options and pass a variable between pages (something.php?type=xxx).  To make it easier, I knew I could use php to grab the types out of my table to list them like this:

 

<?php
$type_set = @mysql_query('SELECT id, type FROM types');
        if (!$type_set) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
        }
while ($row = mysql_fetch_array($type_set)) {
echo "<option value=\"{$row['id']}\">{$row['type']}</option>";
}
?>

 

Since the form action is set before the above php runs, I know I needed a way to reset it with 'type' as my pass through argument.  Does this make more sense?

 

 

Link to comment
Share on other sites

you definitely need javascript to do something like that,although I don't know how.

 

Can you explain why you need a different form action for each selection?

 

The form you have already posts a variable (your row id ) to the form action script.

 

All you have to do then is test which variable has been posted.

 

ie if row 1 is posted do this

  if row 2 is posted do something else.

 

That way you only need to specify a single form action

Link to comment
Share on other sites

I thought I needed a different form action for each because my mind was stuck in HTML mode.  I originally listed the selections in a <a href> tag directing each selection to the same page BUT passing a different argument along.

 

I understand what you are saying though... all I need is a loop and I can test what variable was selected.  I'll try that and let you know how I make out.  Thanks

Link to comment
Share on other sites

Ok, I may be a little stumped.

 

My page doesn't have to be a form (I guess) all I have is one drop-down list and a submit button.  The drop-down list is populated by a list in a database (code above) and all I need is to send the 'type' argument along so that I can use that in another query on another page.

 

As I understood it, in order to pass along an argument, you can put it at the end of the url (something.php?type=x)

Link to comment
Share on other sites

<select name="type">
        <?php
$type_set = @mysql_query('SELECT id, type FROM types');
        if (!$type_set) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
        }
while ($row = mysql_fetch_array($type_set)) {
echo "<option value=\"{$row['id']}\">{$row['type']}</option>";
}
        ?>
</select>

 

But how does my selection get to the form action line?  I MUST be missing something

Link to comment
Share on other sites

You're overcomplicating things.

 

The basic inbuilt function of a form is to pass the selected value to the form action page.

 

Look at this code for a generic selection menu

 

<select name='cat_id' id='cat_id' tabindex='8' class='signupinput2'>
<option value=''>-- Select ---</option>
<option value='1' >Food</option>
<option value='2' >Fashion</option>
<option value='3' >Entertainment</option>
</select>

 

The select name is 'cat_id'. When you select an option, its value is passed along to the form action page.

 

So if a user selects fashion. The value 2 is posted in $_POST['catid']

 

On your form action you can retieve this value by placing it in a variable

 

$category=$_POST['catid']

 

Once you know the value, you can do whatever you like. If you want to specify a different action depending on the selection made. You can just test for which value was passed.

 

if $category=1 do this

if $category=2 do something else

if $category =3 mountain bike out of an aeroplane.

Link to comment
Share on other sites

I understand part of what you're saying but I still can't get it to work

 

<form action="review.php" align="center" method="post">
                <p>Type:
        	<select name="wtype" id="wtype" size="1">
            	 	<?php
		$type_set = @mysql_query('SELECT id, type FROM types');
				if (!$type_set) {
		exit('<p>Error performing query: ' . mysql_error() . '</p>');
		}
		while ($row = mysql_fetch_array($type_set)) {
		echo "<option value=\"{$row['id']}\">{$row['type']}</option>";
		}
		?>
            	        </select>
                        <input type="submit" value="Go">
                        </p>
                        </form>

 

I have "$_GET['type']" on my review.php page and I have a echo line to check it but nothing comes over

Link to comment
Share on other sites

Ok. Easy. If you look at your select menu you have

 

<option value=\"{$row['id']}\">{$row['type']}</option>"

 

The $row['type'] is the 'name' of the select item. ie This is what gets printed on the select menu.

 

$row['id'] is the 'value'. This is the actual piece of information that is posted. You need to use that to process the form.The $GET variable which store this value is the name of your select menu. ie the selection will be passed as $_GET['wtype']

 

This is basic HTML.Remember that PHP is a way of dynamically outputting HTML code. If you don't understand how the HTML is working, PHP will be very difficult for you.

 

 

Link to comment
Share on other sites

  • 3 weeks later...
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.