yami007 Posted March 31, 2009 Share Posted March 31, 2009 well here's everythin i want to know how i can keep a value into a form while reloading the same page with different values to allow me to add multiple entries to the db here's my code : <html> <head> <title>add an entry</title> </head> <body> <form action="add_mutiple_entries.php?n=<?php echo $_GET['n']; ?>" method="post"> The 1 query already active<br /> Name 1: <input type="text" name="name" value="" /><br /> Age 1: <input type="text" name="age" value=""/><br /> <br /> <?php $i = 1; while ( $i <= $_GET['n'] - 1 ) { $i++; echo '<input type="checkbox" name="add' . $i . '" value="' . $i . '"/> <span>The ' . $i . ' query</span><br />'; echo "\n"; echo 'Name ' . $i . ' : <input type="text" name="name' . $i . '" /><br />'; echo "\n"; echo 'Age ' . $i . ' : <input type="text" name="age' . $i . '" /><br />'; echo "\n"; echo "<br />"; } ?> <br /> <br /> <input type="submit" name="submit" value="submit" /><br /> </form> <form action="?"> <label>Add more</label> <select name="n"> <?php if ( $_GET['n'] == 0) { $nb = 1; } else { $nb = 0; } $n = $_GET['n'] + $nb; $i = $n; while ( $i <= 20 - 1) { $i++; $d = $i - 1; echo '<option value="' . $i . '">' . $d . '</option>'; echo "\n"; } ?> </select> <input type="submit" value="add" /> </form> </body> </html> help ^^ Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/ Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 use the value attribute in the inputs and save the value in a session echo 'Name ' . $i . ' : <input type="text" name="name' . $i . '" value="'.$_SESSION['name'].'" /><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797925 Share on other sites More sharing options...
yami007 Posted March 31, 2009 Author Share Posted March 31, 2009 it does not work what i understood from u is : <?php $_SESSION['name'] = $_POST['name']; $_SESSION['age'] = $_POST['age']; ?> and call the session name in the value attribute but when i click the " add " button there i see nothing in the form all empty :s Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797935 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 you must call session_start(); before anything with the sessions is done and before any output has been made. Pretty much, add this to the top of the page: <?php session_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797939 Share on other sites More sharing options...
yami007 Posted March 31, 2009 Author Share Posted March 31, 2009 of course I did it ^^ if u see my code well u'll see that i have 2 forms and 2 buttons so when i click the submit button it submits the query very well and when i click the add button it opens the same page with different value to allow me to add more fields but it does not keep the other form's values.. Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797947 Share on other sites More sharing options...
dadamssg Posted March 31, 2009 Share Posted March 31, 2009 extract($_POST); include("yourform.php"); exit(); and for the values of each text field put @$yourvariable Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797953 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 oh, I see what you are talking about. Well, there isn't a good way of doing this without using JavaScript. Your going to need to grab the input values from your main form and place em in some hidden fields in the add form. One hidden field foreach input in the other form. Then, when they submit the add from, the values of the other from will be in the query string where then you can use em like $_GET['temp_name'] or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-797955 Share on other sites More sharing options...
yami007 Posted March 31, 2009 Author Share Posted March 31, 2009 extract($_POST); include("yourform.php"); exit(); and for the values of each text field put @$yourvariable i dont understand this right ??? Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-798116 Share on other sites More sharing options...
premiso Posted March 31, 2009 Share Posted March 31, 2009 i dont understand this right ??? Do not try to, it is rubbish and bad programming. Can you post your current code (the one you modified to use sessions?)? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-798144 Share on other sites More sharing options...
yami007 Posted April 3, 2009 Author Share Posted April 3, 2009 Can you post your current code (the one you modified to use sessions?)? Thanks. yes here it is : <?php session_start(); ?> <html> <head> <title>add multiple entries !!</title> </head> <body> <form action="add_mutiple_entries.php?n=<?php echo $_GET['n']; ?>" method="post" name="myForm"> The 1 query already active<br /> Name 1: <input type="text" name="name" value="" /> <br /> Age 1: <input type="text" name="age" value="<?php echo $_SESSION['age']; ?>"/><br /> <?php $_SESSION['name'] = $_POST['name']; $_SESSION['age'] = $_POST['age']; ?> <br /> <br /> <?php $i = 1; while ( $i <= $_GET['n'] - 1 ) { $i++; ?> <?php echo '<input type="checkbox" name="add' . $i . '" value="' . $i . '"/> <span>The ' . $i . ' query</span><br />'; echo '<div id="layer1">'; echo "\n"; echo 'Name ' . $i . ' : <input type="text" name="name' . $i . '" /><br />'; echo "\n"; echo 'Age ' . $i . ' : <input type="text" name="age' . $i . '" /><br />'; echo "\n"; echo "</div>"; echo '<br />'; } ?> <br /> <br /> <input type="submit" name="submit" value="submit" /><br /> </form> <form action="?" name="Add"> <label>Add more</label> <select name="n"> <?php if ( $_GET['n'] == 0) { $nb = 1; } else { $nb = 0; } $n = $_GET['n'] + $nb; $i = $n; while ( $i <= 20 - 1) { $i++; $d = $i - 1; echo '<option value="' . $i . '">' . $d . '</option>'; echo "\n"; } ?> </select> <input type="submit" value="add" /> </form> </body> </html> thanks, really need help with this.. Quote Link to comment https://forums.phpfreaks.com/topic/151942-keepin-the-form-values-even-while-reloading-it/#findComment-800762 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.