Jump to content

perfect form


ranonymus

Recommended Posts

hello everyone,

im new here and i need help to complete a job.

the thing is:

i need a form that pulls data from database and then after the submit button it will save it to another table.

the form will have 3 selects that are connected, all of them are filled with a table info. when the first one is choosen the second one will appear and so on. also the form will have to save the text from the selects, not the value:

  echo ("<option value=\"$row[cod_ano]\ " . ($ano == $row["cod_ano"]? " selected" : "" ) . "> ".$row['nome_ano'] . "</option>");
    
 
i need to save the .$row['nome_ano'] but it saves the .$row['cod_ano'], and if i change it the 2nd one doenst work.
this is getting my crazy allready...

can any1 help???

thanks. Edited by ranonymus
Link to comment
Share on other sites

Well, if you want the name to be returned by the user's selection choice on the first one, that is what you must change.

 

As for the cascading effect to bring up the second select box, that is something you'll have to show us first.

Link to comment
Share on other sites

this is what i have, much is missing...

   <?php
    $ano  =  null; //declare vars
    
     
    $conn = mysql_connect("localhost", "root", "");
    $db = mysql_select_db('concrete',$conn);
     
    if(isset($_GET["cod_ano"]) && is_numeric($_GET["cod_ano"]))
    {
    $ano = $_GET["cod_ano"];
    }
    
    
    
    <script language="JavaScript">
    function autoSubmit()
    {
    var formObject = document.forms['theForm'];
    formObject.submit();
    }
    </script>
     
   
     


   <form id ="theForm" name="theForm" method="GET">
   <div style="width:1100px; height:768px;">
     <table width="1090" height="766" border="0">
  <tr>
    <td width="600"></td>
    <td style="margin-top:10px;"><select style="width:160px;"  name="cod_ano" onChange="autoSubmit();">
    <option  value="cod_ano">Selecione o Ano</option>
    <?php
    $sql = "SELECT * FROM ano";
    $array1 = mysql_query($sql,$conn);
    while($row = mysql_fetch_array($array1))
    {
    echo ("<option value=\"$row[cod_ano]\" " . ($ano == $row["cod_ano"]? " selected" : "") . ">$row[nome_ano]</option>");
    }
    ?>
    </select>
   <?php
    if($ano!= null && is_numeric($ano))
    {
?>
   <select style="width:260px;" name="cod_curso" onChange="autoSubmit();">
    <option value="cod_curso">Selecione o curso</option>
    <?php
    $sql2 = "SELECT * FROM curso WHERE cod_ano = $ano ";
    $array2 = mysql_query($sql2,$conn);
    while($row = mysql_fetch_array($array2))
    {
    echo ("<option value=\"$row[cod_curso]\" " . ($curso == $row["cod_curso"]? " selected" : "") . ">$row[nome_curso]</option>");
    }
    ?>
    </select>
    <?php
    if($curso!= null && is_numeric($curso))
    {
?>
     <br>
<br>

     <select style="width:250px;" name="cod_disciplina" onChange="autoSubmit();">
    <option value="cod_disciplina">Selecione a disciplina</option>
    <?php
    $sql3 = "SELECT * FROM disciplina WHERE cod_ano = $ano AND cod_curso = $curso ";
    $array3 = mysql_query($sql3,$conn);
    while($row = mysql_fetch_array($array3))
    {
    echo ("<option value=\"$row[cod_disciplina]\" " . ($disciplina == $row["cod_disciplina"]? " selected" : "") . ">$row[nome_disciplina]</option>");
    }
    ?>
    </select>
   
      <input name="formSubmit" type="submit" value="Enviar" />    
      <input type="reset" value="Limpar" onclick="reset()" />
    </p></td>
  </tr>
</table>
 
Edited by ranonymus
Link to comment
Share on other sites

Lotta code there!

 

It appears that you are using php to check the user's first selection, using the code to look up the values for the second selection, and so on.

 

One way would be to re-query your tables for the names associated with the codes that were selected after all the selections are complete.

 

Another way could be to combine the code and the name in the 'value=' value clause and then when reading it from your POST, explode it into two vars - code and name. Your value clause would look like:

 

$val = $row['code_ano']."|".$row['nomo_ano'];

echo "<option value='$val'......

 

Then explode that using the | char to break it back down.

Link to comment
Share on other sites

so your saying to do this

 

<option  value="cod_ano">Selecione o Ano</option>
    <?php
    $sql = "SELECT * FROM ano";
    $array1 = mysql_query($sql,$conn);
    while($row = mysql_fetch_array($array1))
    {
    echo ("<option value=$val = $row['code_ano']."|".$row['nomo_ano'] " . ($ano == $row["cod_ano"]? " selected" : "") . ">$row[nome_ano]</option>");
    }
    ?>
    </select>

 

is this???

and how do i get only the name?..

what if we use a script to get the text from the select instead of the value?

Link to comment
Share on other sites

I can't follow the complex option statement. That's why I did it in two lines

 

$val = $row['code_ano']."|".$row['nomo_ano'];

Then use that $val to make the option easier to read:

 

echo "<option value='$val' " . $ano == $row["cod_ano"]? " selected" : "" . ">$row[nome_ano]</option>";

 

Now your screen will show the name in the dropdown and your php will get something like this:

 

$code_ano = $_GET['code_ano'];

list($ano,$ano_name) = explode("|",$code_ano);

 

$ano will have the code; $ano_name will have the title.

Link to comment
Share on other sites

Don't know what you are asking for.

 

My idea simply gives you what you want to have - the code in order to make your next select box work and the name of that code which you said you also needed. You'll have to figure out how to implement it and then show us what you have.

 

When you say it doesn't work, what do you mean? Do you have an error message? (do you have error reporting turned on?)

Link to comment
Share on other sites

so, i cant get the values in order for now like this

 

 

<select style="width:160px;"  name="cod_ano" onChange="autoSubmit();">
    <option  value="cod_ano">Selecione o Ano</option>
    <?php
    
    $sql = "SELECT * FROM ano";
    $array1 = mysql_query($sql,$conn);
    while($row = mysql_fetch_array($array1))
    {
    $val = $row['code_ano']."|".$row['nome_ano'];
    echo ("<option value=\"'$val'\" " . ($ano == $row["cod_ano"]? " selected" : "") . ">$row[nome_ano]</option>");
    }
    ?>
    </select>

 

but now it doens´t load the 2nd select... and i think that is because the script for cascade....

 

<script language="JavaScript">
    function autoSubmit()
    {
    var formObject = document.forms['theForm'];
    formObject.submit();
    }
    </script>
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.