ranonymus Posted June 26, 2013 Share Posted June 26, 2013 (edited) 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 June 26, 2013 by ranonymus Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 Your value= clause is the culprit. Change the name of the table column and you'll have it. Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 26, 2013 Author Share Posted June 26, 2013 like i said, if i change it the 2nd select doenst work... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 26, 2013 Author Share Posted June 26, 2013 (edited) 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 June 26, 2013 by ranonymus Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 26, 2013 Author Share Posted June 26, 2013 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 26, 2013 Author Share Posted June 26, 2013 sorry i cant make it work now, could you pls make the select with your exemple? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 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?) Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 26, 2013 Author Share Posted June 26, 2013 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> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 You have too many quotes in your option lines - you only need the single quotes around $val, not the escaped double quotes as well. See my previous example please. Quote Link to comment Share on other sites More sharing options...
ranonymus Posted June 27, 2013 Author Share Posted June 27, 2013 if i use your example i get no value Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 27, 2013 Share Posted June 27, 2013 PLEASE - show the new code when you want us to help solve your problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.