plastik77 Posted August 7, 2007 Share Posted August 7, 2007 Hi, I've got a frustrating problem which i can't seem to resolve. Basically, I have tabulated data pulled from a database, however, one of the fields I have made an editable text field so that this field can be changed by the user. Therefore, the user can edit the text fields, click the Submit Changes button and the page should refresh showing the table of data with the changes having taken effect. One problem - the submit button has to be clicked twice before u see the changes on the page. This is because of the position of the update statement in the loop below which is definitely wrong, however, I can't how work out how else to resolve this as i need access to the variables and the text field values which must be generated before i can do my update statement. New to PHP so any help would be great. <form action="<? $_SERVER['PHP_SELF'] ?>" method ="post" > <? $query = mysql_query("select* from TestQuestion t,Question q, Section s WHERE t.testID = '$_SESSION[test]' and q.questID = t.questID and s.sectionID = t.sectionID and s.testId = '$_SESSION[test]' order by questNumber asc"); $numrows = mysql_num_rows($query); for ($i=0;$i<$numrows;$i++) { $row = mysql_fetch_array($query); $qID = $row['questID']; /process update if form submitted if(isset($_POST['Submit'])) { //update with the new quest number $update = mysql_query("update TestQuestion set questNumber ='$_POST[$qID]' where questID ='$qID'"); } $questNum = $row['questNumber']; $sectName = $row['sectionName']; $qtext = $row['quesText']; $cat = $row['category']; $weight = $row['weight']; echo "<tr>"; echo "<th>Q: <input type='text' name='$qID' value='".$questNum."' size='2'></th>"; echo "<th>".$qtext."</th></tr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63687-solved-update-statement-within-a-loop-haviing-to-submit-twice-to-see-changes/ Share on other sites More sharing options...
matto Posted August 7, 2007 Share Posted August 7, 2007 You need to arrange you code differently. <?php if(isset($_POST['submit'])) { //do the updating.... } ?> then the sql select and html form goes here..... Quote Link to comment https://forums.phpfreaks.com/topic/63687-solved-update-statement-within-a-loop-haviing-to-submit-twice-to-see-changes/#findComment-317421 Share on other sites More sharing options...
NArc0t1c Posted August 7, 2007 Share Posted August 7, 2007 I would personally use ajax or frames for this. Then it could be updated faster and without reloading. I just can't remember the ajax..., sorry. Quote Link to comment https://forums.phpfreaks.com/topic/63687-solved-update-statement-within-a-loop-haviing-to-submit-twice-to-see-changes/#findComment-317446 Share on other sites More sharing options...
plastik77 Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks for the replies. For the moment, I will avoid using AJAX as this is a simple prototype. I realise I need to have the script at the top of the page, but I'm not sure how best to access the values in the text field, when I try to use access the posted textfield value, I get the undefined index error for $qID. Any idea how i can access the array of values? f(isset($_POST['submit'])) { //do the updating.... echo $_POST['$qID']; } Quote Link to comment https://forums.phpfreaks.com/topic/63687-solved-update-statement-within-a-loop-haviing-to-submit-twice-to-see-changes/#findComment-317473 Share on other sites More sharing options...
plastik77 Posted August 7, 2007 Author Share Posted August 7, 2007 Got it working - thanks for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/63687-solved-update-statement-within-a-loop-haviing-to-submit-twice-to-see-changes/#findComment-317502 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.