jaydeesmalls Posted April 29, 2008 Share Posted April 29, 2008 Hi Everyone, I have a form that when submitted, sends the data into the database. What I am looking for is that when someone later comes back to this form, the current data from the database is in the text fields, able to be edited, and resent to the database. Does anyone know how to do this? Thank you all very much. Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/ Share on other sites More sharing options...
Rowno Posted April 29, 2008 Share Posted April 29, 2008 Easy, just insert the data from the database into value="" attribute of each input tag. Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529264 Share on other sites More sharing options...
jaydeesmalls Posted April 29, 2008 Author Share Posted April 29, 2008 But if the data changed, wouldn't I have to manually change the <input value=" ">? Is there a way that it would automatically show the data in the text field the next time I came back to change the data. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529275 Share on other sites More sharing options...
DeanWhitehouse Posted April 29, 2008 Share Posted April 29, 2008 by doing a MySql query, and echoing the result in the <input value="<?php echo"$value"; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529277 Share on other sites More sharing options...
Rowno Posted April 29, 2008 Share Posted April 29, 2008 Just like he said ^, just do a mysql query to the database to request the changed data and then echo it out into the value attributes of the input fields Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529280 Share on other sites More sharing options...
Fadion Posted April 29, 2008 Share Posted April 29, 2008 Others said it, but im giving a real example so u understand it right: <?php //update the db info if the form is submitted. This "if" part can aslo be in another page. if(isset($_POST['text'])){ if($_POST['text'] != ''){ $text = $_POST['text']; $textid = $_POST['id']; $resultsUpdate = mysql_query("UPDATE table SET text='$text' WHERE id=$textid"); echo 'The text was updated successfuly'; } else{ echo 'The field cant be empty.'; } } $id = 10; //just for the example's sake $results = mysql_query("SELECT text FROM table WHERE id=$id"); $values = mysql_fetch_array($resuslts); ?> <form id="myForm" name="myForm" method="post" action="mypage.php"> <input type="text" name="text" value="<?php echo $values['text']; ?>" /> <input type="hidden" name="textid" value="<?php echo $id; ?>" /> <input type="submit" name="Submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529301 Share on other sites More sharing options...
jaydeesmalls Posted April 29, 2008 Author Share Posted April 29, 2008 Thanks everyone. What about for drop down menus. Where in: <select name="in2"> <option value=" "> </option> <option value="Person1">Person1:</option> <option value="Person2">Person2:</option> <option value="Person3">Person3:</option> <option value="Person4">Person4:</option> </select> Would I fit the previous answer? Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529336 Share on other sites More sharing options...
Rowno Posted April 29, 2008 Share Posted April 29, 2008 Well I guess you got the <input>s working then? Anyway, they're a bit different but still simple, since you can't just change the value, instead do something like this: <option value="Option1" <?php if ($nameofvariableforthisselect == "Option1") echo 'selected="selected"'; ?>>Option1</option> This will cause the option to be selected if the variable is equal to it's value. Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529338 Share on other sites More sharing options...
jaydeesmalls Posted April 29, 2008 Author Share Posted April 29, 2008 I got the inputs working. Thanks a lot for that. You saved me a bigger headache. I'm not quite getting what you're saying about the part you just input. <?php if ($nameofvariableforthisselect == "Option1") echo 'selected="selected"'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529340 Share on other sites More sharing options...
Rowno Posted April 29, 2008 Share Posted April 29, 2008 Just insert that in the <option> tag like shown and replace Option1 with the value of the <option> you put it in and replace the $nameofvariableforthisselect variable with the variable containing the value you took from the database like you did for the <input>s. And do that for every <option> tag. Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529345 Share on other sites More sharing options...
jaydeesmalls Posted April 29, 2008 Author Share Posted April 29, 2008 I spoke too soon. It worked perfectly. thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/103343-how-do-i-make-an-editable-form-that-populates-current-data-from-mysql/#findComment-529346 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.