Jump to content

[SOLVED] Function Won't Update Info On Same Page As Info Added


Recommended Posts

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 -

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."'");

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.

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?

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>

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

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?

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?

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>

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! :]

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"
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.