vomitbomb Posted May 14, 2008 Share Posted May 14, 2008 Is it possible to pull values from the database and put them into forms? I want to pull up a specific order and repopulate the order forms so that order can be changed or updated. Thanks. Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/ Share on other sites More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 You would do something like this: <?php //include the connections include 'db.php'; //make the proper query "Safely" (this is just simple for time sake $sql = mysql_query("SELECT * FROM tableName WHERE id = '{$_GET['id']}'"); if(mysql_num_rows($sql) > 0){ $row = mysql_fetch_array($sql); echo'<form action="#" method="post"> <input type="text" name="text1" value="'.$row['column1'].'" /> <input type="submit" value="SEND!" /> </form>'; }else{ echo 'Invalid ID'; } ?> Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540932 Share on other sites More sharing options...
vomitbomb Posted May 14, 2008 Author Share Posted May 14, 2008 Ok I'm trying to figure it out, I realise it sends it to the forms, but how can I write in on the orderform so the forms get populated with these results? Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540964 Share on other sites More sharing options...
jonsjava Posted May 14, 2008 Share Posted May 14, 2008 show us the code, and I'll rework it to do it for you. Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540966 Share on other sites More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 I don't know if your explaining this correctly or what, but the above code will populate the text field with what ever is in the database. Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540969 Share on other sites More sharing options...
vomitbomb Posted May 14, 2008 Author Share Posted May 14, 2008 ah k I get what you're explaining, nah I need this to send this to another document. $fname = $_POST['fname']; echo $fname; echo '<label>First <input type="text" name="fname" id="fname" value="$fname"/> </label>' Ok the echo up there gives me the right information, although on this particular form it just leaves $fname instead of it's value. I've also tried value=' echo $fname; '> Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540972 Share on other sites More sharing options...
vomitbomb Posted May 14, 2008 Author Share Posted May 14, 2008 Cool I managed to get the value into the form, but now I can't reset the form because the value is set to $fname. :-\ <input type="text" name="fname" id="fname" value='<? echo $fname;?>'> and the reset: <input type="reset" value="Reset Fields" /> Any idea on how I can reset this? Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540983 Share on other sites More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 Try this: function resetFields(){ var inputs = document.getElementsByTagName('input'); for(var i=0;i < inputs.length;i++){ inputs[i].value = ''; } } <input type="reset" value="Reset Fields" onclick="resetFields()" /> Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-540991 Share on other sites More sharing options...
vomitbomb Posted May 14, 2008 Author Share Posted May 14, 2008 function resetFields(){ var inputs = document.getElementsByTagName('input'); for(var i=0;i < inputs.length;i++){ inputs[i].value = ''; } } am I meant to be changing something there? causes an error on the second line. Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-541003 Share on other sites More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 OK, with the reset button, it just sets it to its original state, you have to use a button to do this: <script type="text/javascript"> function resetFields(){ var inputs = document.getElementsByTagName('input'); for(var i=0;i < inputs.length;i++){ if(inputs[i].getAttribute("type") == "text"){ inputs[i].value = ''; } } } </script> <form name="form1"> <input type="text" value="cat" /> <input type="button" value="Clear" onclick="resetFields()" /> </form> Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-541054 Share on other sites More sharing options...
vomitbomb Posted May 15, 2008 Author Share Posted May 15, 2008 ok this works well for the first name, but not for anything else.. hmm //Update order echo'<form action="order.php" method="post"> <input type="hidden" name="fname" value="'.$row['fname'].'" /> <input type="hidden" name="lname" value="'.$row['lname'].'" /> <input type="submit" value="Update-Order" /> </form>'; and then: <label>First <input type="text" name="fname" id="fname" value='<? echo "$fname" ?>' /> </label> <label>Surname <input type="text" name="lname" id="lname" value='<? echo "$lname" ?>' /> </label> Anyone know why? Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-541511 Share on other sites More sharing options...
vomitbomb Posted May 15, 2008 Author Share Posted May 15, 2008 ok I got that to work. Didn't like the include I used for all the $_POST for some reason. This only works on text menus and wont work on list menus, anyone know why? Link to comment https://forums.phpfreaks.com/topic/105593-pulling-values-from-the-database-and-putting-them-into-forms/#findComment-541516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.