Jump to content

Increment Variable When Click On Submit


neptunerain

Recommended Posts

Hello everybody, first of all i need to say that this is my first topic and i need to enfasize that my english is so bad 'cause i'm from Brazil.

 

Ok, now let's talk about my "problem".

 

 

Well, i'm trying to make a form where the user can click in 2 different buttons and these buttons increase variables that correspond with it.

 

 

Something like:

 

<?php
if(isset($_POST['zelda'])){
$zelda++;

}

if(isset($_POST['mario'])){

$mario++;

}

?>
<html>
<head></head>
<body>
<form action="" method="post">
<?php   echo "Zelda has"  .  $zelda  . "Votes";	  ?>
<br>
<?php echo  "Mario has "  .  $mario . "Votes"; ?>
<br><br>
<input type="submit" name="mario" value="Vote in Mario!">
<br><br>
<input type="submit" name="zelda" value="Vote in Zelda!">
</form>
</body>
</html>

 

 

It doesn't work 'cause the variables keep returning their values to 0 over and over again.

 

My friend sad i could use sessions to keep it with some value when refreshing the page.

 

I'm a beginner so i dont have any idea how to solve this.

 

Oh, i tried using sessions but the page become blank and thats it.

 

 

Sorry about anything, i just came to here because i think this forum has more users than all from brazil.

 

 

 

Bye.

Link to comment
Share on other sites

Do you want to register (and remember) the clicks of all visitors that come to your site, or do you only want to register the clicks that 1 user did?

 

If you want to register the clicks of all visitors then you need a "place on your server" to save your current values. That can be in a database or in a file on your server.

 

If you want to register the clicks off 1 user you can use sessions. The session value is stored on the clients computer and is unique for each visitor.

Edited by drunkenMonkey
Link to comment
Share on other sites

Alright i understood. In this case the data wont be individual so forget about sessions right?

 

Well, i don't want to use database 'cause there aren't stuff to put in it, just a number.

 

I'll search around the internet how to store the value in a file on my server.

 

Thank you by guide me in the correct way.

Edited by neptunerain
Link to comment
Share on other sites

Well, i don't want to use database 'cause there aren't stuff to put in it, just a number.

 

That makes absolutely no sense.

 

<?php
// connection stuff
$sql = mysql_query("SELECT * FROM `Votes`");

$row = mysql_fetch_assoc($sql);

$zelda = $row['zelda'];
$mario = $row['mario'];

if(isset($_POST['zelda'])){
$zelda++;

$sql = mysql_query("UPDATE `Votes` SET `zelda` = '$zelda'");

echo "Voted for zelda successfully!";

}

if(isset($_POST['mario'])){

$mario++;

$sql = mysql_query("UPDATE `Votes` SET `mario` = '$mario'");

echo "Voted for mario successfully!";

}

?>
<html>
<head></head>
<body>
<form action="" method="post">
<?php echo "Zelda has" . $zelda . "Votes";	 ?>
<br>
<?php echo "Mario has " . $mario . "Votes"; ?>
<br><br>
<input type="submit" name="mario" value="Vote in Mario!">
<br><br>
<input type="submit" name="zelda" value="Vote in Zelda!">
</form>
</body>
</html>

Edited by SocialCloud
Link to comment
Share on other sites

I dont know if it makes sense because as i sad before i'm from brazil , my english is so bad.

 

 

But basically i'm thinking of not using any database to do what i want to do because i just want to store a number and not a large amount of data.

 

 

Can you understand me now?

 

Sorry guys but its my best.

Link to comment
Share on other sites

First off: Don't use single quotes around field names, only string values.

 

Secondly, this is the proper syntax for adding to a column:

UPDATE `votes` SET `zelda` = `zelda` + 1

 

Notice that I've used backticks where you've used single quotes. As this is the proper way to escape field names. The backticks are optional, however, unless you're trying to use a reserved word. I prefer to always use them, just to make it 100% certain that I will not have any conflicts with current or future reserved words.

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.