# [web] (solved) [php] passing variable to itself

This topic is 4114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am writing a small page that pass the variable "id" to itself. The page is here: A character list Supposedly, when you click on a character, the link would pass the value of "id" to the same page, and display additional info of that character. But when I test that page, it seems that the value of "id" is null when I click the link.
<html>
<body>

<?php

$db = mysql_connect("localhost", root); mysql_select_db("ecwai_wwwdb",$db);
$result = mysql_query("SELECT * FROM characters",$db);

while ($rowdata = mysql_fetch_row($result)) {

// Name, Title
printf("<a href=\"%s?id=%s\">%s the %s</a><br/>", $PHP_SELF,$rowdata[0], $rowdata[1],$rowdata[2]);

}

/* If I have this following line, the **** line will print. */
// $id = 3; if($id){
printf("**** You have clicked on a specific character.<br/><br/>");
}

?>

</body>
</html>


If I put $id=3 before the if statement, the line **** would printf at the end. Which makes me think that the variable$id is not passed to the page when it reloads. What did I do wrong in the code? Do you see any other thing that I should change in my code? - Thanks [Edited by - Wai on June 15, 2007 7:47:13 PM]

##### Share on other sites
One of the things I see wrong is that at no point are you pulling the value of id.

When you look at a URL, all the extra information included at the end of a file are your $_GET data (for PHP at least). To pull this information and put it into a variable, you should do the following: if(isset($_GET['id']))	$id =$_GET['id'];
By checking if the array item even exists, you're avoiding coming across any PHP notices or errors.

If there is no character selected, then select everything, when a specific character is selected, do you still want everything selected and displayed? That's how it's currently going to work.

I haven't really used printf(), but I'm sure the 2nd usage of it is unnecessary, especially since you're printing straight text.

##### Share on other sites
Thank you Matha, that fixed the problem.

I haven't decided on the layout yet, I think I will use the complete list as an index on the same page across the top or on the side (I haven't put the div tags) :)

##### Share on other sites
I have a similar problem with the submit button, where php doesn't recognize that the button has been pressed:

[00] <html><body>[01] <?php[02] if($submit || isset($_GET['submit'])){[03]   printf("The submit button has been pressed.");[04] }else{[05]   printf("Please press the submit button.<br/>");[06]   printf("<input type=\"Submit\" name=\"submit\" value=\"Submit\">");[07] }[08] ?>[09] </body></html>

Test page

It never get to line 3, unless I put ?submit=1 manually at the url. How do I fix this code ?

- Thanks

EDIT:
It seems that I now need to use isset($_POST['submit']). #### Share this post ##### Link to post ##### Share on other sites Submits only work on forms. I suggest you read up on how to use forms so that you can use them properly. Also, PHP does not always turn a form field into a variable. You're pulling$submit based on two factors:

1) There's a submit field on the web page
2) You're assuming PHP has been configured to make variables out of the form fields.

It's best not to assume PHP is configured to behave how you want expect it to behave, which is why you perform error checking so that your code doesn't fail.

Also, you only need to use printf() if you're replacing items in a string, like you have on your first example. For what you're using it for here, it's pointless. You should use either echo or print.

##### Share on other sites
I think the confusion comes from an old PHP feature which is now disabled by default, register_globals. With this system, data available to PHP was automatically copied into variables - rather than the newer system of them being accessible through special "superglobal" arrays.

In this instance, the $_GET array contains data sent to the script via the URL. Forms can be used to submit data using GET or POST methods. I suggest you use POST. You also use the variable$PHP_SELF. Again, this has been moved to the $_SERVER superglobal (ie,$_SERVER['PHP_SELF']).

Your PHP tutorial/book seems rather out of date.

An example for form submission could be:

<?phpif (isset($_POST['some_button'])) { echo 'Some button was clicked.';} else { ?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">        <input type="submit" name="some_button" value="Hello" />    </form>    <?php}?>

##### Share on other sites
Thank you for pointing out the differences. The tutorial I have been following is out of date.

I some occassions, I want to conditionally display an html form. Right now I am using this kind of code:

<?php if($mode==1){ // echo and printf for form 1...}else if($mode==2){  // echo and printf for form 2...}else{  // echo and printf for form 3...}?>

Is there a way such that I don't need to use echo and printf, and use <?php ?> tags inline instead ?

##### Share on other sites
Quote:
 Original post by WaiThank you for pointing out the differences. The tutorial I have been following is out of date. I some occassions, I want to conditionally display an html form. Right now I am using this kind of code:Is there a way such that I don't need to use echo and printf, and use tags inline instead ?

PHP allows you to go in and out of the code (probably all languages allow this, I can't say for certain though).

You code would be like this:
<?php if($mode==1) { ?><form action="" method="post"><h2>Form 1</h2></form><?php } else if($mode==2) { ?><form action="" method="get"><h2>Form 2</h2></form><?php } else { ?><form action="" method="post"><h2>Form 3</h2></form><?php } ?>

You can also print data in a form without having to go with full PHP tags:
<?php$name = '';if($_POST){    if(isset($_POST['name']))$name = $_POST['name'];}?><form action="" method="post"><h2>Form 1</h2><input type="text" name="name" value="<?=$name?>" /></form><?php } ?>

This way if you want to allow a user to submit a form, but keep the data they posted without making them retype it, it will automatically pull it in. There are much better ways to do it, but this is a starting ground to accomplishing something like this.

Thank you !

##### Share on other sites
Just a reference: PHP: Basic syntax

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 33
• 13
• 12
• 10
• 12
• ### Forum Statistics

• Total Topics
632573
• Total Posts
3007144

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!