serhio
serhio

Reputation: 28586

Submit onChange in PHP

How should I do correctly refresh the page in PHP on a "select" element?

In my homework, I have 2 comboboxes 1) vehicle type and 2) specific to that type colors Each time I select a type I need to refrech the linked colors.

It seems that onchange="this.form.submit()" does not work!

this is my code:

<?php
include_once ("config.php");

$link = mysql_connect($host,$user,$pass) or die("Could not connect : " . mysql_error());
mysql_select_db($db_name) or die("Could not select database");
?>
<html>
<head>
</head>
<body>
<?php
$menu_type=$_POST['menu_type'];
echo 'menu_type='.$menu_type.'<br/>';
echo 'empty='.(bool)empty($menu_type).'<br/>';
echo '-1='.(bool)($menu_type==-1);
?>
<table>
    <form name="myform" action="ajoute.php" method="POST" >
    <tr>
        <td>Matricule</td>
        <td><input type="text" value="" style="width:150px;" name="Matricule"></td>
    </tr>
    <tr>
        <td>Vehicule</td>
        <td>Couleur</td>
    </tr>
    <tr>
        <td><select name="menu_type" style="width:150px;" onchange="this.form.submit()">
        <option value="-1">Select voiture type</option>
            <?php
            $resType = mysql_query("SELECT * FROM type_vehicule") or die(mysql_error()); 
            for ($i=0; $i<mysql_num_rows($resType); $i++)
            {
                $row=mysql_fetch_array($resType);
                echo "<option value = ".$row['ID'].">".$row['nom']."</option>";

            }?>
        </select>       
        </td>
        <td>
        <?php
        echo "empty = ".(bool)empty($menu_type)."; $menu_type = ".$menu_type;
        ?>
        <select name="menu_couleur" size="1" style="width:150px;">

        <?php       

        if (empty($menu_type) || $menu_type == -1) 
        {
            echo "<option value='-1'>Select voiture type</option>";
        }
        else
        {
            $type=(int)$menu_type;
            $result=mysql_query("SELECT c.Id, c.Couleur FROM couleur c inner join
            type_couleur tc on c.id=tc.Couleur_ID where tc.type_id=".$type);
            while ($row = mysql_fetch_array($result)) { 
            echo "<option value=".$row['ID'].">".$row['couleur']."</option>";  
            } 
    } 
?>
        </select>
        </td>
    </tr>
    <tr>
        <td align="right" colspan=2><input type="submit" name="submit" value="Valide"></td>
    </tr>
    </form>
</table>

<?php

mysql_free_result($resType);
mysql_close($link);
?>
</body>
</html>

Upvotes: 0

Views: 7457

Answers (2)

AjayR
AjayR

Reputation: 4179

First of all, you have written the form inside the table tag, which is not correct. it should be like this

<form>
<table>
...
</table>
</form>

Get the output in HTML and see if you get any javascript errors in the console for easy identification. Also check the Submit button name.

Upvotes: 0

rlorenzo
rlorenzo

Reputation: 1368

The problem is because you named your submit button as submit (i.e. name="submit"). So when you do a this.form.submit(), it is trying to perform a function on an html element.

In chrome javascrip console I get the error:

document.formname.submit is not a function Uncaught TypeError: Property 'submit' of object #HTMLForm element is not a function

Did a google search and came up with this page that explained everything: http://eee94180.blogspot.com/2008/09/javascript-formsubmit-is-not-function.html

So you fix your problem just remove name="submit" in your submit button or name it something else, like name="submitButton".

Upvotes: 1

Related Questions