Jump to content

Archived

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

Tjk

Passing a parameter into a query string

Recommended Posts

Code in main page...
[code]
<?
$var= 1;
?>
<form method="POST" action="map.php">
<input type="image" src="imagecreate2.php?var='$var'" height="300" width= "300" />
</form>
[/code]

Code in image create...
[code]

  if(isset($_GET['var'])){
    $var= $_GET['var'];
  }

  mysql_query("SELECT * from table WHERE id='$var'") or die(mysql_error());

//does something to an image and outputs it using imagepng($img);
[/code]

The problem is that the image now doesn't load on the main page. Could anyone point out where I'm going wrong?

Regards
-Tjk

Share this post


Link to post
Share on other sites
Can you post the code that sends the image back to the browser. You are probably missing the header() fucntion that sets the correct output type.

Ken

Share this post


Link to post
Share on other sites
Sure. Here it is....

[taken from imagecreate2.php]
[code]
<?
  $im= imagecreatefrompng('maptemp.png');
  
  if(isset($_GET['battle'])){
    $battle= $_GET['battle'];
  }
  
  mysql_connect("localhost", "user", "password")or die(mysql_error());
  mysql_select_db("database");
  
  $query= mysql_query("SELECT * from battles WHERE battle='$battle'")or die(mysql_error());
  while($row= mysql_fetch_array($query)){
    $unit= $row['unit'];
    $unitx= $row['xcoord'];
    $unity= $row['ycoord'];
    
    
    $lcoordx= $unitx- 5;
    $lcoordy= $unity - 5;
  
    $rcoordx= $unitx + 5;
    $rcoordy= $unity + 5;
    
    $color= imagecolorallocate($im, 0xFF, 0xFF, 0xFF);  
    imagefilledrectangle($im, $lcoordx, $lcoordy, $rcoordx, $rcoordy, $color);
  }
  imagepng($im);
?>
[/code]

Share this post


Link to post
Share on other sites
You're missing the "header()" function that tells the web browser to expect image data:
Add the following line to your funtion
[code]<?php    header('Content-type: image/png'); ?>[/code] before the line [code]<?php imagepng($im); ?>[/code]

Ken

Share this post


Link to post
Share on other sites
I've added the header() function just above imagepng($im) and it doesn't work.

Before I added the parameter into the query string for imagecreate2.php when I was just using a mysql statement with no WHERE condition everything was working fine and the map loaded. It was only when I added the parameter and to allow for the WHERE condition that it no longer works. Any ideas?

Help appreciated as always.

Share this post


Link to post
Share on other sites
i don't see in your script where you are passing the battle variable to your imagecreate2.php script. it seems as though when you do this:

if(isset($_GET['battle'])){
$battle= $_GET['battle'];
}

$_GET['battle'] is not set, and therefore $battle is never set, either, so when you do your query, it selects nothing.

edit: did you mean to do $_GET['var'] ?

Share this post


Link to post
Share on other sites
if it's purely a query page do this
if (isset ($_GET['battle'])) $battle = $_GET['battle']; else die ("Missing Variables");

or just incapsulate the entire code in the $_GET['battle'] if() function

Share this post


Link to post
Share on other sites
@ Crayon Violent: Earlier in the script the variable $battle is defined. Sorry, the original code I posted was a test script I set up to test if it was something wrong with my code. Everything is identical except in my example I used $var instead of $battle. $battle is definately set within the main script and ready to pass via the query string into the image creation script.

I'm using a script called 'map.php' and through the img src I'm calling the script imagecreate2.php to generate the image.

@joquius: I need the variable $battle to be set at all times so an else isn't really an option.

Share this post


Link to post
Share on other sites
I just took a closer look at the original code you posted:
[code]<?php
$var= 1;
?>
<form method="POST" action="map.php">
<input type="image" src="imagecreate2.php?var='$var'" height="300" width= "300" />
</form>[/code]
Notice that the "<input>" line is outside the PHP tags, so you are passing the literal string "[b]'$var'[/b]" to your script, not the value of [b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$var[!--colorc--][/span][!--/colorc--][/b].
What you really should have there is:
[code]<input type="image" src="imagecreate2.php?var=<?php echo $var; ?>" height="300" width= "300" />[/code]
If you look at the generated source before and after you've made this change, you should see the difference.

Ken

Share this post


Link to post
Share on other sites
ooh good eye, [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Share this post


Link to post
Share on other sites

×

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.