Forum Topic: a button that adds 1 to a php varia

(219 views • 27 replies)

This topic is 1 page long.

<< < > >>
None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 10:01 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

how do i make it when you press a lets say plus button (+) it adds 1 to a certain variable in php??

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 8/20/08 10:09 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

Um... can you elaborate a bit? Would you like to add one to a variable on the page that's been sent from php, or would you like to reload the page with a variable incremented? You'll need javascript for the first one and to use a superglobal array like post for the second.

Sufficiently advanced incompetence is indistinguishable from malice.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 10:14 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

ile try to give a example
<?php
$number =1;
?>
javascript codething $number+=1
<?php
echo $number;
?>

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 8/20/08 10:20 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

At 8/20/08 10:14 AM, Dad00 wrote: ile try to give a example
<?php
$number =1;
?>
javascript codething $number+=1
<?php
echo $number;
?>

Well first of all you don't have to use the 'javascript codething $number += 1'

if you're doing it in php just do:

<?
$number = 1;
$number ++;
echo $number;
//that will echo '2'
?>

None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 10:27 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

HUH?!?!?! i said i wanted it so when u click a button it adds 1 to a the variable

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 8/20/08 10:46 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

The thing is, PHP is run on the server of your site and html is run on your visitors computer. Except in some very special circumstances, your html can't interact with your php. Your php can interact with your html just before it is sent to the browser, but not afterwards.

HTML can talk to PHP through $_GET and $_POST variables: both involve loading a new page. All the ?id=1343345356&foo=bar crap you get at the end of urls are $_GET variables. You could do something like this:

Current number is <?php echo $_GET['myvar'] ?>
<form method="get" action="this_page.php">
  <input type="hidden" name="myvar" value="<?php echo $_GET['myvar'] + 1 ?>" />
  <input type="submit" value="Increment!" />
</form>

Sufficiently advanced incompetence is indistinguishable from malice.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 10:55 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

how do u do a variable in html is it the same way as php?? $number = 1;??

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 8/20/08 11:02 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

HTML doesn't have variables. It's a markup language rather than a programming languages. It doesn't have ifs, it doesn't have variables it just sort of sits there. However you can capture user input using forms (like I did above). When someone clicks the submit button it will send the data to php, depending on the method="" attribute on the <form> tag, the data will either be in $_POST or $_GET variables in PHP.

The example above has method="get" so it send the value of myvar to $_GET['myvar']. It's then incremented in PHP every time.

You can also embed javascript inside of html. This can be used to pass variables between html and php, but I wouldn't recommend it yet. It's pretty complicated. But you can very easily use it to do things like increment a value in the page itself, without the server knowing about it,

Sufficiently advanced incompetence is indistinguishable from malice.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 11:05 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

Notice: Undefined index: myvar in E:\web\webserver\htdocs\button.php on line 4

i need to define the variable and when i click the button it goes to this page with a very long url

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 11:07 AM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

For fucks sake learn how to spell.

Posted from Linux. Distro may vary.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 11:10 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

excuse me?

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 11:10 AM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

<?php
if ($_GET['n']) {
	$n = $_GET['n'];
	$n2 = $n + 1;
} else {
	$n = 0;
	$n2 = 1;
}
echo "The number is currently ".$n;
echo "<form action=\"index.php\" method=\"get\">";
echo "<input type=\"hidden\" name=\"n\" value=\"$n2\" />";
echo "<input type=\"submit\" value=\"+1\" /></form>";
?>

You can see it in action here.

Posted from Linux. Distro may vary.


None

authorblues

Reply To Post Reply & Quote

Posted at: 8/20/08 11:15 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 8/20/08 11:10 AM, Wonderful wrote: echo "<form action=\"index.php\" method=\"get\">";
echo "<input type=\"hidden\" name=\"n\" value=\"$n2\" />";
echo "<input type=\"submit\" value=\"+1\" /></form>";

you people who go through all the trouble of escaping everything, instead of just breaking out of the php tags. its really a ridiculous amount of extra work that makes the source so much more unreadable. congrats.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 11:19 AM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

At 8/20/08 11:15 AM, authorblues wrote: you people who go through all the trouble of escaping everything, instead of just breaking out of the php tags. its really a ridiculous amount of extra work that makes the source so much more unreadable. congrats.

I'm not sure about you or anyone else, but I can read it fine. I escape things by habit though.

But whatever:

<?php
if ($_GET['n']) {
	$n = $_GET['n'];
	$n2 = $n + 1;
} else {
	$n = 0;
	$n2 = 1;
}
?>

The number is currently <?php echo $n; ?>
<form action="index.php" method="get">
<input type="hidden" name="n" value="<?php echo $n2; ?>" />
<input type="submit" value="+1" /></form>

Posted from Linux. Distro may vary.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 11:25 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

is there a more secure way?? im making a text game for a school project and people could easily cheat by changing the number on the url....

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 11:28 AM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

At 8/20/08 11:25 AM, Dad00 wrote: is there a more secure way?? im making a text game for a school project and people could easily cheat by changing the number on the url....

Could try using POST rather than GET.

<?php
if ($_POST['n']) {
	$n = $_POST['n'];
	$n2 = $n + 1;
} else {
	$n = 0;
	$n2 = 1;
}
?>

The number is currently <?php echo $n; ?>
<form action="index.php" method="post">
<input type="hidden" name="n" value="<?php echo $n2; ?>" />
<input type="submit" value="+1" /></form>

Posted from Linux. Distro may vary.


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 11:34 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

ty soo much

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 11:56 AM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

buuut could someone explain to me how the code works??

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

DearonElensar

Reply To Post Reply & Quote

Posted at: 8/20/08 12:14 PM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

Post is just as easily "hacked".
There is no way to do it secure unless you start using sessions to make sure they don't add more then 1 or whatever.

BBS Signature

None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 12:24 PM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

At 8/20/08 12:14 PM, DearonElensar wrote: Post is just as easily "hacked".
There is no way to do it secure unless you start using sessions to make sure they don't add more then 1 or whatever.

Well this is for a school project, I doubt people will be trying to hack the game.

Posted from Linux. Distro may vary.


None

Wonderful

Reply To Post Reply & Quote

Posted at: 8/20/08 12:27 PM

Wonderful FAB LEVEL 13

Sign-Up: 07/27/08

Posts: 854

Explaining:

if ($_POST['n']) {
	$n = $_POST['n'];
	$n2 = $n + 1;
} else {
	$n = 0;
	$n2 = 1;
}

$n is the number that is being shown, and $n2 is the next number in the sequence. $_POST['n'] will be true if the form is submitted. If there is no form submitted, the current number ($n) will be 0, and the next number ($n2) will be 1.

Sorry if that makes little or no sense.

Posted from Linux. Distro may vary.


None

Relish

Reply To Post Reply & Quote

Posted at: 8/20/08 12:28 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 780

At 8/20/08 11:56 AM, Dad00 wrote: buuut could someone explain to me how the code works??

sure thing

<?php
// Check for post.
if ($_POST['n']) {
// If so, assign post[n] to a variable
$n = $_POST['n'];
// Add one to $n.
$n2 = $n + 1;
} else {
// If not, make the number 0 and add 1 to get one.
$n = 0;
$n2 = 1;
}
?>

<!-- Output -->
The number is currently <?php echo $n; ?>
<!-- The form -->
<form action="index.php" method="post">
<input type="hidden" name="n" value="<?php echo $n2; ?>" />
<input type="submit" value="+1" /></form>


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 12:38 PM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

ty every1 im also planning to expand this project into a actual game eventually dont tell me the code ile need but how do i make it very 'secure' sessions?? or what

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

Relish

Reply To Post Reply & Quote

Posted at: 8/20/08 12:44 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 780

i suggest getting a php book

for super secure sessions you need to have a secure connection (https)

and thats about it


None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 12:53 PM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

i have got a php book i just want to know what i need to look at

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

DearonElensar

Reply To Post Reply & Quote

Posted at: 8/20/08 01:19 PM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

With sessions it would be something like

<?php
session_start();
if ($_GET['n']) {
	$n = $_GET['n'];
	$n2 = $n + 1;
	$n3 = $_SESSION['n3'];
	$_SESSION['n3'] = $n2;
} else {
	$n = 0;
	$n2 = 1;
	$_SESSION['n3'] = $n2;
}

if ($n3 == $n)
{
?>
The number is currently <?php echo $n; ?>
<form action="index.php" method="get">
<input type="hidden" name="n" value="<?php echo $n2; ?>" />
<input type="submit" value="+1" /></form>
<?php
} else{
?>
You cheated!

You write the number you except to a session, and then you check if the number you except is equal to the number you get back.
If there is a difference we say the user is a cheater :)

BBS Signature

None

Dad00

Reply To Post Reply & Quote

Posted at: 8/20/08 01:38 PM

Dad00 LIGHT LEVEL 04

Sign-Up: 05/30/07

Posts: 634

will ssl do the job? the web server im going to use will have ssl

MY website www.gamingrounds.co.uk (under construction) As:Main
I now do games/website for people very cheaply!!! for more info pm me


None

DearonElensar

Reply To Post Reply & Quote

Posted at: 8/20/08 02:13 PM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

At 8/20/08 01:38 PM, Dad00 wrote: will ssl do the job? the web server im going to use will have ssl

SSL has nothing to do with this.
SSL encrypts the data so that third party's cannot see what data is being sent (that is why you should always use it when working with credit cards for example, only the intended party's get the credit card data).
But in this case you have no problems with third party's, you have to worry about the first party, or in other words the user.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 11:23 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!