Jump to content

[SOLVED] how do i change variables with button clicks?


cornishmouse

Recommended Posts

I'm only a beginner at php and i don't know alot yet so what i'm asking is probably shockingly simple so please bear with me. i'm making a text based game and what i'm stuck on is making a button click change a value in a variable ive tried looking at tutorials but i cant find anything helpful if you could explain it to me or give me an example that would be great  or  a link to a tutorial that you think will help 

 

 

Consider the following code:

 

$myVar = 'sword';
echo "<a href=\"mypage.php?myvar=axe\">Change my var</a>";
if(isset($_GET['myvar']) and $_GET['myvar'] == 'axe'){
     $myVar = mysql_real_escape_string($_GET['axe']);
     echo $myVar; //it will print 'axe'
}

 

What im doing with that code is initialize $myVar with the value 'sword'. The "<a href ..." part prints a link with a url variable which then i check if it is set. If so $myVar will become as the url variable, 'axe'. The $_GET is the key to capture url variables. Hope its clear.

I understand everything except the mysql_real_escape_string  what is that doing exactly? ??? 

 

is it possible to put this in a buttton? i know it has a onclick="" feature  but im not sure how to put anything inside can i just put it in as code or do i need to make a function and call it?

From php manual:

 

Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.

 

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

 

This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.

 

The onclick event is used to call javacript code or functions. Make an html link as in the sample code and when u click it, it sends to the url the variable which can be captured with $_GET. Hope its clear now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.