%24 Oh the Humanity!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

verycleanteeth
Forum Newbie
Posts: 8
Joined: Thu Mar 11, 2004 1:15 am

%24 Oh the Humanity!

Post by verycleanteeth »

When I run the following code I get this address:
http://www.verycleanteeth.com/PHP/Game. ... ER3=Female

Can anybody tell me why it's adding those bloody %24 buggers next to all my form values? My code would work fine if I could just strip them from the URL.

Code: Select all

<form action="game.php" method=get>
<?php
$num=1;
while ($num < 4) 
{ 
$SpecA='SPECIES$num';
$GenA='GENDER$num';
$NamA='NAME$num';
	echo "<td>$num</td>
	<td width=20><input type="text"name="$NamA"size="15"maxlength="15"></td>
	<td width=20><select name="$SpecA">
		<option>Pac</option>
		<option>Blob</option>
		<option>German</option>
		<option>Penguin</option>
	</select></td>
	<td width=20><select name="$GenA">
		<option>Male</option>
		<option>Female</option>
		<option>Transexual</option>
	</select></td>
</tr>";
$num++;
}
?>
also, can anybody point me to a good get/post method tutorial? Everytime I try to send anything with the post method I get an error page. Get works just fine, but is too abuseable for my tastes.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

are you sure that is the output you are getting from this piece of code? the %24 is the URL encoding for the $ sign ....

try changing the code to ....

Code: Select all

<form action="game.php" method=get>
<?php
$num=1;
while ($num < 4)
{
$SpecA='SPECIES' . $num;
$GenA='GENDER' . $num;
$NamA='NAME' . $num;
   echo "<td>$num</td>
   <td width=20><input type="text" name="$NamA" size="15" maxlength="15"></td>
   <td width=20><select name="$SpecA">
      <option>Pac</option>
      <option>Blob</option>
      <option>German</option>
      <option>Penguin</option>
   </select></td>
   <td width=20><select name="$GenA">
      <option>Male</option>
      <option>Female</option>
      <option>Transexual</option>
   </select></td>
</tr>";
$num++;
}
?>
and see what happens!
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If that works here is why....

the double quote string declaration for strings is evaluated.
the single quote string declaration is not.

Therfore

$test="there";
'hello$test' == hello$test
"hello$test" == hellothere

Processing wise the single quotes are quicker as they are not parsed but you need to concatenate any variables to them, not insert them within the string.
verycleanteeth
Forum Newbie
Posts: 8
Joined: Thu Mar 11, 2004 1:15 am

Post by verycleanteeth »

taking the variables outside the quotes solved my %24 problem. One problem remains though, Now i'm getting this URL when I run the code:
http://www.verycleanteeth.com/PHP/Game. ... Transexual

I don't understand why it's adding size%3D%2215%22maxlength%3D%2215%22
to my name fields.

Here is my updated code:

Code: Select all

<?php
$num=1;
while ($num < 4) 
{ 
$SpecA='SPECIES'.$num; 
$GenA='GENDER'.$num;
$NamA='NAME'.$num; 
	echo "<td>".$num."</td>
	<td width=20>".'<input type="text" name="'.$NamA.'" size="15" maxlength="15"></td>
	'."<td width=20><select name=".$SpecA.">
		<option>Pac</option>
		<option>Blob</option>
		<option>German</option>
		<option>Penguin</option>
	</select></td>
	<td width=20><select name=".$GenA.">
		<option>Male</option>
		<option>Female</option>
		<option>Transexual</option>
	</select></td>
</tr>";
$num++;
}
?>

?>
Thanks a bunch for the help BTW :D
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

what have you done! try ...

Code: Select all

<?php 
$num=1; 
while ($num < 4) 
{ 
$SpecA='SPECIES'.$num; 
$GenA='GENDER'.$num; 
$NamA='NAME'.$num; 
   echo "<td>" . $num . "</td> 
   <td width=20><input type='text' name='" . $NamA . "' size='15' maxlength='15'></td> 
   <td width=20><select name='" . $SpecA . "'> 
      <option>Pac</option> 
      <option>Blob</option> 
      <option>German</option> 
      <option>Penguin</option> 
   </select></td> 
   <td width=20><select name='" . $GenA . "'> 
      <option>Male</option> 
      <option>Female</option> 
      <option>Transexual</option> 
   </select></td> 
</tr>"; 
$num++; 
} 
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

It is adding all of the %20 (etc) characters because that is how browsers work! It is just URL Encoding the values submitted from the form.

It is perfectly correct, you do not need to "stop" it happening - what you do probably need to do is convert the data back to "normal" text again, so just urldecode it (literally: $var = urldecode($value_from_form).

Also if you used the POST method on your form (instead of GET) - it wouldn't do any of this at all :)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

launchcode, have another look at the URL and the variables that are there and you will realise that something is not working properly!

yes the%3D and %24 are normal for URL encoding but there is still a problem.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You're not terminating your " and ' characters properly so it's passing the whole input tag on the query string because it thinks it is all the name.

Here try this - your code, just made a little easier to cope with:

Code: Select all

<?php
$num = 1;

while ($num < 4)
{
	$SpecA = "SPECIES$num";
	$GenA = "GENDER$num";
	$NamA = "NAME$num";
?>
<td><?=$num?></td>
<td width=20><input type="text" name="<?=$NamA?>" size="15" maxlength="15"></td>
<td width=20>
<select name="<?=$SpecA?>">
<option>Pac</option>
<option>Blob</option>
<option>German</option>
<option>Penguin</option>
</select>
</td>
<td width=20>
<select name="<?=$GenA?>">
<option>Male</option>
<option>Female</option>
<option>Transexual</option>
</select>
</td>
</tr>
<?
	$num++;
}
?>
verycleanteeth
Forum Newbie
Posts: 8
Joined: Thu Mar 11, 2004 1:15 am

Post by verycleanteeth »

thanks launchcode. That code did the trick, I didn't realize you could go in and out of php like that in the middle of a while statement.

an appreciative person is me! :)
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

Not always a good idea to break out of PHP like that. Don't forget this method:

Code: Select all

<?php 
$num = 1; 

while ($num < 4) 
{ 
   $SpecA = "SPECIES$num"; 
   $GenA = "GENDER$num"; 
   $NamA = "NAME$num"; 
 
echo <<<RANDOMTEXTSTRING
<td>$num</td> 
<td width=20><input type="text" name="$NamA" size="15" maxlength="15"></td> 
<td width=20> 
<select name="$SpecA"> 
<option>Pac</option> 
<option>Blob</option> 
<option>German</option> 
<option>Penguin</option> 
</select> 
</td> 
<td width=20> 
<select name="$GenA"> 
<option>Male</option> 
<option>Female</option> 
<option>Transexual</option> 
</select> 
</td> 
</tr> 
RANDONTEXTSTRING;
   $num++; 
} 
?>
You may also use curly braced around variable names which enables you to put array values within the html (if you had any).

Code: Select all

<?php 
$num = 1; 

while ($num < 4) 
{ 
   $SpecA = "SPECIES$num"; 
   $GenA = "GENDER$num"; 
   $NamA = "NAME$num"; 
 
echo <<<RANDOMTEXTSTRING
<td>{$num}</td> 
<td width=20><input type="text" name="{$NamA}" size="15" maxlength="15"></td> 
<td width=20> 
<select name="{$SpecA}"> 
<option>Pac</option> 
<option>Blob</option> 
<option>German</option> 
<option>Penguin</option> 
</select> 
</td> 
<td width=20> 
<select name="{$GenA}"> 
<option>Male</option> 
<option>Female</option> 
<option>Transexual</option> 
</select> 
</td> 
</tr> 
RANDOMTEXTSTRING;
   $num++; 
} 
?>
And even better would be to place all the HTML generated into a single variable:

Code: Select all

<?php 
$num = 1; 

while ($num < 4) 
{ 
   $SpecA = "SPECIES$num"; 
   $GenA = "GENDER$num"; 
   $NamA = "NAME$num"; 
 
$htmlContent .= <<<RANDOMTEXTSTRING
<td>$num</td> 
<td width=20><input type="text" name="$NamA" size="15" maxlength="15"></td> 
<td width=20> 
<select name="$SpecA"> 
<option>Pac</option> 
<option>Blob</option> 
<option>German</option> 
<option>Penguin</option> 
</select> 
</td> 
<td width=20> 
<select name="$GenA"> 
<option>Male</option> 
<option>Female</option> 
<option>Transexual</option> 
</select> 
</td> 
</tr> 
RANDOMTEXTSTRING;
   $num++; 
}

echo $htmlContent;
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

"Not always a good idea to break out of PHP like that."

Other than the fact that is exactly what PHP is designed for - you mind explaining why?

"curly braced around variable names" - are in the code posted pointless and should only ever be used around array elements and only when the string has been quoted (heredoc, echo, etc).

"And even better would be to place all the HTML generated into a single variable:"

Again - why? So you can use up a little more server memory with un-necessary overhead in this instance?
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

In all honesty, my primary reason is this article
http://www.devarticles.com/c/a/PHP/JV%9 ... -part-1/1/
You can't deny that getting rid of the php start and end tags has made things a bit neater and easier to read.

I'm fairly sure the curly braces, although a pointless operation in this example are necessary when it comes to putting $_SESSION['vars'] or $multi['dimesional']['arrays'] in slabs of code like that. It's certainly a necessity should one of your variables run up against another letter.
How is PHP supposed to know where $myvariableendsifthereisanotherletternexttoit?

And as for sticking it all in a variable, whilst in the above example it may not be necessary it would be if I wanted the code to be generated by a function. Let's say that that form list was dynamic:

Code: Select all

<?php

function form_list($selected)
{
    foreach($result as $key=>$val)
    {
        $output .= "<option value="$key"".($key==$selected?" selected":"").">".$val."</option>";
    }
return $output;
}

$num = 1;
$formOptions = form_list(3); 

while ($num < 4) 
{ 
   $SpecA = "SPECIES$num"; 
   $GenA = "GENDER$num"; 
   $NamA = "NAME$num"; 

$htmlContent .= <<<RANDOMTEXTSTRING 
<td>$num</td> 
<td width=20><input type="text" name="$NamA" size="15" maxlength="15"></td> 
<td width=20> 
<select name="$SpecA"> 
$formOptions
</select> 
</td> 
<td width=20> 
<select name="$GenA"> 
<option>Male</option> 
<option>Female</option> 
<option>Transexual</option> 
</select> 
</td> 
</tr> 
RANDOMTEXTSTRING; 
   $num++; 
} 

echo $htmlContent; 
?>
And besides once you have that chunk of code you could use a regular expression on the one variable and parse it for all sorts of things like email addresses, capitalisation, tag removal. If it was in a search result you could highlight the search criteria within the page....
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

"You can't deny that getting rid of the php start and end tags has made things a bit neater and easier to read. "

It depends where you do your development. If (like a lot of people) you use an IDE that can syntax high-light HTML correctly (like Homesite, Zend Studio, etc) then breaking out of PHP allows this functionality - which embedding it into your code does not.

As for being "neater" it's a matter of personal preference, there's no sound programming/logic argument for it.

You have to weigh things up, the same way of doing something isn't always right for every situation.

"I'm fairly sure the curly braces, although a pointless operation in this example are necessary when it comes to putting $_SESSION['vars'] or $multi['dimesional']['arrays'] in slabs of code like that."

Yes agreed, you need them if you want to quote multi-dimensional array elements directly in a string otherwise you get a syntax error! But your example didn't! :) It was just wrapping a single var in {} which is, I'm sorry, utterly pointless.

"And as for sticking it all in a variable, whilst in the above example it may not be necessary it would be if I wanted the code to be generated by a function."

Well, yes.. or if you wanted to pre-process the data before displaying it. But unless you actually need to do that, it's still overhead that has no direct benefit. Even in your example above it's pointless sticking it into $htmlContent because all you're doing is echoing it right out again! Might as well just do that in the first place.

I see where you are coming from and they're viable means to handle html output (and there are many other ways) - just not necessarily relevant to the origins of this thread.

In PHP you can skin the same cat a milion different ways!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

leenoble_uk: Just reading the article you pointed to us, i am immediately discrediting it because of this quote
A user on a 56k modem or even a T1 on the other side of the world will not notice the extra execution time
Since when has PHP execution time been dependant on modem speed!?!?!

Mark
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Heh.. never :) it's also a totally invalid point because BOTH the T1 user and the 56ker will notice if the page takes longer to render just because there is PHP overhead slowing it down in the first instance.
Post Reply