Jump to content

Making A Store Script


Amster

Recommended Posts

Hello,

 

I've created a script that should remove 400 points when I click a button "Buy Item". The problem is that when I run this script, it removes the 400 points, but continues to do so every time I load the page whether or not I click the "Buy Item" button. Also, it shows my warning asking if I want to buy an item but seems to disregard my choice and purchase the item either way. How can I fix this?

 

I simply want a script that when I click the button, it removes 400 points, reloads the page displaying the new amount of points I have, and works properly!

 

Thanks!

 

EDIT: I should note that when I have under 400 points, it does not subtract anymore. This part actually works properly.

Here's the code:

 <h5 class="titlehdr">Store<br></h5>
<?php $rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'"); ?>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) { ?>
<font size="2">
Points: <b> <?php
$points= $row_settings['points'];
echo $points;
$checker = 0;
} ?></b></p>
<?php
if($_POST['Purchase1'] == 'Purchase Item'){
if ($points > 400){ ?>
<script>
var r=confirm("Are you sure you want to buy this?");
if (r){
<?php
$checker = 1;
?>
}
</script> <?php
if ($checker == 1){
mysql_query("update users set points = points - 400
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());
$_POST['Purchase1'] = '';
$checker=0;
header("Location: /store.php");
}
}
} ?>
<td width="196" valign="top"> </td>
<tr>
<form name="store" method="post" id="store">
 <table width="80%" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
 <p align="center">
Item 1<br> 400 points<br><br>
	 <input name="Purchase1" type="submit" id="Purchase1" value="Purchase Item">
 </p>
 <p>  </p>
 </form>
<td colspan="3"> </td>
</tr>
</table>
</body>
</html>

Edited by Amster
Link to comment
Share on other sites

You can use

<?php
if(isset($_POST['Submit'])) {
// execute
} else {
// error
?>

where "Submit" would be the name you assigned to your submit button, to check whether the submit button has been clicked. However some browsers have difficulties with it sometimes

Edited by ExtremeGaming
Link to comment
Share on other sites

I'm not exactly sure if it's the submit button at fault. The thing is I go to the page initially and reload, no points are removed. It's only after I click the submit button once, it removes 400 points but then after that if I keep refreshing the page without clicking the button, it continues to keep removing 400 points on each refresh. Its like once I press the "submit" button once, it does it automatically on each refresh.

Edited by Amster
Link to comment
Share on other sites

Wait, so you reload after you purchase, then you're resending your form. It's natural, your ignoring of the alert message is normal because all the form data you had just sent was resent when you reloaded. I'm not much of a javascript expert so I'll leave it to someone else.

Edited by ExtremeGaming
Link to comment
Share on other sites

But why would it reload when I reload the form? If I didn't click the button again when I reload the site, why should it?

 

Also I have feeling the confirmation button isn't work as it's reading the php first and set $checker=1 before it reads the javascript but I'm not sure how to correctly fix this.

 

Thanks for your help anyway though :)

Link to comment
Share on other sites

Taking a closer look at your code:

 

<?php $rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'"); ?>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) { ?>
<font size="2">
Points: <b> <?php
$points= $row_settings['points'];
echo $points;
$checker = 0;
} ?>

 

You do not need a while loop as long as there are no users with the same user_id. Which is also a bad idea. This can be changed to:

 

<?php $rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'"); ?>
<?php $row_settings = mysql_fetch_array($rs_settings); ?>
<font size="2">
Points: <b> <?php
$points= $row_settings['points'];
echo $points;
$checker = 0;
} ?>

 

Also

header("Location: /store.php");

will cause an error because you're redirecting after you ouput html Edited by ExtremeGaming
Link to comment
Share on other sites

My server collects error logs I think. I decided to scrap the confirm part anyhow, I just want the points to subtract properly. I tried doing this, which sort of works, it actually removes 400 points but delete the submit button for unknown reasons in the process. Then when I refresh again, everything seems fine. I don't know why the submit button disappears though.

 

if($_POST['Purchase1'] == 'Purchase Item'){
if ($points > 400){
mysql_query("update users set points = points - 400
   WHERE id='$_SESSION[user_id]'
  ") or die(mysql_error());
$_POST['Purchase1'] = ' ';
header("Location: admin.php");
exit();
}
} ?>

Link to comment
Share on other sites

You can't use a javascript control structure to assign a php variable a value like you try to do in your code. Javascript is executed client-side, and php is executed server side. Do you have error_reporting set up and working?

 

This-- not to mention you don't want to use a header location redirect. A simple self posting form that checks that the form was submitted, will work properly. You simply execute your decrement when the form is submitted at the top of the script, only when the form is submitted.

 

If you want something fancier, you could use ajax to handle the decrement of points, and returning of the latest count, but I'd recommend highly the use of jquery if you want to go down that route.

Link to comment
Share on other sites

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.