Hello,

I need to submit a form as soon as selection has been changed and call PHP function. I know I can do this with onchange="this.form.submit(); but this will generate an url similar to this:

?submit_position=change

while I need to generate something like this:

?p=edit-product.php&action=change_pos&id=$id

This is what my form look like atm

<form name="position" method="GET" action="?p=edit-product.php&action=change_pos&id=$id"> //?p will include file in my content area and ?action should change the position value in my database
			<select onchange="this.form.submit();">
				<option name="position" value="1" $sel[1]> Firste</option>
				<option name="position" value="2" $sel[2]> Second</option>
				<option name="position" value="3" $sel[3]> Third</option>
				<option name="position" value="4" $sel[4]> No Display esilehel</option>
				<input type="submit" name="position" value="change" class="hidden" />
			</select>
			
		<form>

Any help is appreciated

Recommended Answers

All 3 Replies

A few notes about how you code the form:

1. form action attribute should point to a page (either html or php or # - self). In your example it does not point to a valid url

2. select element should have a name not the option elements so <select name="position"> 3. what is the role of $sel array elements in options?

4. input element should not have the same name as the select element

5. input element should be outside the select element

The names of your element will show up in the query string together with their values. If you would like to pass any extra value you can use hidden field.

<input type="hidden" name="p" value="edit-product.php">

But I do not quite understand the question. Maybe edit-product.php should be your action url.

3. what is the role of $sel array elements in options?

It will output the "selected='selected' when the option is selected. The check is performed before the forms. And just to mention the form is in <<<HEREDOC syntax

2. select element should have a name not the option elements so <select name="position">

4. input element should not have the same name as the select element

5. input element should be outside the select element

Will change them

But I do not quite understand the question. Maybe edit-product.php should be your action url.

The idea is that I have a selection form with 4 options. Now whenever user has changed the option I want the value in the database to be changed.

The ?p is including pages in the <div id="content"></div>

the ?action will trigger an action fucnction and in this case tells it to change the post position field in the database and to SET it equal to ?id.

If ?p is not there then I dont see the selection at all. If the ?action is not there, then I cant change the values. So I need all of those three variables to be set with just one click, I m trying to do it with javascript onchange, but I have ran into dead end. I hope I expressed myself more clear this time. Thanks for your answer.

This is how I would do it.

<html>
<head></head>
<body>

<?php

// initialize value for the position
$pos = '';

// initialize string for content
$content = '';

// check if there is a value for position select in the $_GET
// and position is integer and within expected range (a bit of security)
// (this means that the form was submited by onchange event)
if(isset($_GET['position']) and is_numeric($_GET['position']) and 
         $_GET['position'] > 0 and $_GET['position'] <= 4) {

    // store position in a variable
    $pos = $_GET['position'];

    // a query to change the database (whatever it is)
    $qry = "UPDATE positions SET position=$pos WHERE ...";
    
    // execute the query
    // ...
    
    // DEBUG:
    // display the query to see whether it is OK
    echo "<p><pre>$qry</pre></p>";
}

// array holding the option values and texts
$option_arr = array('1' => 'First',
                    '2' => 'Second',
                    '3' => 'Third',
                    '4' => 'No Display...');

// start a string holding the form
// set action to itself
$form = '<form method="GET" action="#">';

// add select element
$form .= '<select name="position" onchange="this.form.submit();">';

// add options
foreach($option_arr as $value => $text) {

    // start the option tag
    $form .= '<option value="' . $value . '"';

    // if a submited value equals $value then add seleted attribute to the option
    if($value == $pos) {
    
        $form .= ' selected="selected"';
    }
    
    // add the text and end the option tag 
    $form .= ">$text</option>";
}

// end the select tag and the form tag
$form .= '</select></form>';

// display the form
echo $form;

// add the divs for displaying content (here or elsewhere) and the content
// (I do not know where do you get content from)
echo '<div id="content">' . $content . '</div>';

?>

</body>
</html>

Note:
- you do not need a submit button if the onchange event will autosubmit the form.
- I set the action to # so the form submits to the same page, when submitted the script checks whether there is a value and if the value is in the range
- the query is not finished since I do not know the fields and conditions
- debug code displays the query so you can check if it is what you expect
- the values for the select element are in an array so makes the script flexible
- include the edit-product.php wherever and whenever necessary (either in the beginning of the script or in the beginning of the if check)
- handle the content the way you want (I do not know exactly where it comes from)

commented: Good post, cleared some things out +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.