CloudSex13 Posted February 26, 2009 Share Posted February 26, 2009 Hey there - Thanks for reading. I'm having trouble with the following code - What the code is doing right now is allowing a user to add a "thing" into the database, which it does, and then it requires an additional page refresh from the user in order to show the newly added info. What I'm trying to get the code to do is allow a user to not have to refresh the page an extra time in order to show the newly added info (kind of like when you click the "Preview" button when submitting a new topic on these forums, it updates without the need of an extra page refresh by the user to display the content you've modified.) Is there a way for me to successfully do this with my code? I've tried messing around with the { and }'s in various different places, but that performed no luck. I've also reduced my code, so if there are any odd variable mismatches (which I don't believe there are any), then that would be why. But the code itself is fully functional. Thank you very much for any help if so. <?php include('config.php'); function add() { if (isset($_POST['add'])) { $addthing = $_POST['thing']; $addamount = $_POST['amount']; mysql_query("INSERT INTO Things SET ThingID='', Thing='$addthing', Amount='$addamount' WHERE AccountID='".$AccountID."'"); $added = "<div class=box>The thing has been added.</div>"; } echo " Add Thing<br> ".$added." <div class=box> <form method=post action=test.php?action=add> Thing: <textarea name=thing cols=35 rows=5></textarea> <br><br> Amount: <input type=text name=amount> <br><br> <input type=submit value='Add' name=add> </form> </div>"; } $things = mysql_query("SELECT * FROM Things WHERE AccountID='".$AccountID."'"); $allthings = 1; while ($getthings = mysql_fetch_array($things)) { $ThingID = $getthings['ThingID']; $Thing = $getthings['Thing']; $Amount = $getthings['Amount']; if ($allthings == 1) { $displaythings .= " ".$Thing."<br> Amount: <b>".$Amount."</b>"; $allthings = 2; } else { $displaythings .= " ".$Thing."<br> Amount: <b>".$Amount."</b>"; $allthings = 1; } } ?> <html> <head> <title>Test</title> <link rel=stylesheet type=text/css href=styles.css> </head> <body> <div align=center> <div id=container> <div class=box> Things </div> <a href=test.php?action=add>Add</a><br> <?php if (isset($_GET['action'])) { $action = ($_GET['action']); if ($action == "add") { add(); } else { echo "That category does not exist."; } } ?> <?php echo $displaythings; ?> </div> </div> </body> </html> Best - Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/ Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 I've been searching Google thoroughly, but I've still had no luck... :/ Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-771966 Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 Anyone...? Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772031 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 Your query is bad. mysql_query("INSERT INTO Things SET ThingID='', Thing='$addthing', Amount='$addamount' WHERE AccountID='".$AccountID."'"); Should be mysql_query("INSERT INTO Things (ThingID, Thing, Amount, AccountID) VALUES ('', '$addthing', '$addamount', '".$AccountID."'"); Or you really meant to update it: mysql_query("UPDATE Things SET ThingID='', Thing='$addthing', Amount='$addamount' WHERE AccountID='".$AccountID."'"); Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772034 Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 Would that really be the reason for the extra refresh required by the user? Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772042 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 Would that really be the reason for the extra refresh required by the user? Nope, and looking at insert into I guess you can do it the way you had it, weird. The reason it requires another page refresh is because you call the function "add" after the data processing has been done. Move that portion above the data fetching portion and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772047 Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 Are you suggesting that I should place my info query statement inside my add statement? Add is at the top of the page, so shouldn't it process the data after it sends a query for it, because add is before the data fetching code? Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772049 Share on other sites More sharing options...
MadTechie Posted February 26, 2009 Share Posted February 26, 2009 No move it up like below.. that way it add to the DB before get the data from the DB <?php include('config.php'); function add() { if (isset($_POST['add'])) { $addthing = $_POST['thing']; $addamount = $_POST['amount']; mysql_query("INSERT INTO Things SET ThingID='', Thing='$addthing', Amount='$addamount' WHERE AccountID='".$AccountID."'"); $added = "<div class=box>The thing has been added.</div>"; } echo " Add Thing<br> ".$added." <div class=box> <form method=post action=test.php?action=add> Thing: <textarea name=thing cols=35 rows=5></textarea> <br><br> Amount: <input type=text name=amount> <br><br> <input type=submit value='Add' name=add> </form> </div>"; } if (isset($_GET['action'])) { $action = ($_GET['action']); if ($action == "add") { add(); } else { echo "That category does not exist."; } } $things = mysql_query("SELECT * FROM Things WHERE AccountID='".$AccountID."'"); $allthings = 1; while ($getthings = mysql_fetch_array($things)) { $ThingID = $getthings['ThingID']; $Thing = $getthings['Thing']; $Amount = $getthings['Amount']; if ($allthings == 1) { $displaythings .= " ".$Thing."<br> Amount: <b>".$Amount."</b>"; $allthings = 2; } else { $displaythings .= " ".$Thing."<br> Amount: <b>".$Amount."</b>"; $allthings = 1; } } ?> <html> <head> <title>Test</title> <link rel=stylesheet type=text/css href=styles.css> </head> <body> <div align=center> <div id=container> <div class=box> Things </div> <a href=test.php?action=add>Add</a><br> <?php echo $displaythings; ?> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772051 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 Add is at the top of the page, so shouldn't it process the data after it sends a query for it, because add is before the data fetching code? The function call is what I meant. And no a function being defined is just that. It is a set of responses that when called it processes it. The function declaration could be at the end of the page but what matters is where you call the function, as Mad was kind to show you with an example. function Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772054 Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 Thank you, MadTechie! Also, thanks premiso - I didn't quite grasp what you meant, but that makes sense now. Also, by switching over to your solution MadTechie, the page ends up placing the form out of the container at the top of the page now. Should I make the function and error a variable and place it appropriately on the page where I'd like it to go? Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772056 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 Also, by switching over to your solution MadTechie, the page ends up placing the form out of the container at the top of the page now. Should I make the function and error a variable and place it appropriately on the page where I'd like it to go? Instead of echoing out, why not store the output in a variable then return that variable and echo that out where you want it to go in the script? Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772057 Share on other sites More sharing options...
MadTechie Posted February 26, 2009 Share Posted February 26, 2009 Maybe try this! <?php include ('config.php'); if (isset($_GET['action'])) { if ($_GET['action'] == "add") { if (isset($_POST['add'])) { $addthing = $_POST['thing']; $addamount = $_POST['amount']; mysql_query("INSERT INTO Things SET ThingID='', Thing='$addthing', Amount='$addamount' WHERE AccountID='" . $AccountID . "'"); $added = "<div class=box>The thing has been added.</div>"; } } else { $added = "<div class=box>That category does not exist.</div>"; } } $things = mysql_query("SELECT * FROM Things WHERE AccountID='" . $AccountID . "'"); $allthings = 1; while ($getthings = mysql_fetch_array($things)) { $ThingID = $getthings['ThingID']; $Thing = $getthings['Thing']; $Amount = $getthings['Amount']; if ($allthings == 1) { $displaythings .= "$Thing<br>Amount: <b>$Amount</b>"; $allthings = 2; } else { $displaythings .= "$Thing<br>Amount: <b>$Amount</b>"; $allthings = 1; } } ?> <html> <head> <title>Test</title> <link rel=stylesheet type=text/css href=styles.css> </head> <body> <?php echo " Add Thing<br>$added <div class=box> <form method=post action=test.php?action=add> Thing: <textarea name=thing cols=35 rows=5></textarea> <br><br> Amount: <input type=text name=amount> <br><br> <input type=submit value='Add' name=add> </form> </div> <div align=center> <div id=container> <div class=box>Things</div> <a href=test.php?action=add>Add</a><br> $displaythings </div> </div> "; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772061 Share on other sites More sharing options...
CloudSex13 Posted February 26, 2009 Author Share Posted February 26, 2009 Instead of echoing out, why not store the output in a variable then return that variable and echo that out where you want it to go in the script? Can you store a function as a variable? I.e. $variable = add(); ----------- MadTechie, thank you for the example. I'll have a test run when I get home! :] Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772070 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 Instead of echoing out, why not store the output in a variable then return that variable and echo that out where you want it to go in the script? Can you store a function as a variable? I.e. $variable = add(); AS long as the function returns something. <?php function myFunc() { return "Test"; } $val = myFunc(); echo $val; // echos "Test" ?> Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772071 Share on other sites More sharing options...
CloudSex13 Posted February 27, 2009 Author Share Posted February 27, 2009 Works bomb - Thanks premiso and MadTechie! Quote Link to comment https://forums.phpfreaks.com/topic/147040-solved-function-wont-update-info-on-same-page-as-info-added/#findComment-772267 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.