using coordinates from input image in an html form

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

Post Reply
karimD
Forum Newbie
Posts: 4
Joined: Fri Apr 07, 2006 5:42 pm

using coordinates from input image in an html form

Post by karimD »

Please help me!

I've been working on a research-type website involving various html forms that are processed using PHP and the resulting data is written to text files.
One of the pages requires the user to click somewhere on an image, based on which area most applies to them. This image is being used as an input type in an html form. The code used for this form is as follows:

Code: Select all

<form action="/formHandler/saveIOS.php" method="post">

<b><center>Inclusion of Other in Self Scale</center></b>

<input type = "hidden" name = "page" value = "IOS">

Please click where in the diagram best corresponds to how close you feel to your partner.<br>
<input type = "image" src = "/images/IOS.gif" name = "image1">

</form>
For some reason, I can't seem to write the coordinates of the location the user clicked, into these text files, nor can I echo them to display on screen. Please read on for details and code examples.

When using the POST method, the resulting array shown on screen upon submission of the form contains the correct values when I add the following code to the beginning of my form handler file.

Code: Select all

echo '<pre>'; 
print_r($_POST);
echo '</pre>';
However, when I try to write the vales into a text file, nothing gets written.
I have tried assigning the coordinate values (X and Y) to variables such as below:

Code: Select all

$imageX = $_POST['image1_X'];
$imageY = $_POST['image1_Y'];
$fileIn = fopen($fileName, 'a') or die("Unable to open file!");

$lineText = "X = ".$imageX.", Y = ".$imageY."\n\n";
fwrite($fileIn, $lineText);
fclose($fileIn);
but all that writes to the file is "X = , Y = "

I also tried POSTing the values directly into the fwrite function as below:

Code: Select all

$fileIn = fopen($fileName, 'a') or die("Unable to open file!");
fwrite($fileIn, $_POST['image1_X'];
fclose($fileIn);
but nothing writes to the file. (Please note: I'm not sure if this way is even possible but I tried it anyway.)

Since the values are clearly being stored in the POST array correctly, my initial assumption was that somehow I wasn't storing the values correctly in the variables. However, the initial html form also passes a variable called "page" which I was able write to the text file.

Please note that I did try all of the above steps using the GET method and got the same resuts.

At this point I have searched multiple newsgroups and FAQs for a similar thread somewhere but to no avail.
Still, I am convinced that it's not possible that I am the first person to have ever had this problem.
As such I am posting this in hopes someone can help.

Thank you for your consideration.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'll assume the parse error in your last snippet is a posting typo.

try using var_dump() on the two values from your second snippet. They may be there, but PHP may think they are blank. You can try using strval() or intval() against them as well to see if the information comes out correctly.
karimD
Forum Newbie
Posts: 4
Joined: Fri Apr 07, 2006 5:42 pm

Post by karimD »

Thanks for responding so quickly feyd. I really appreciate that.

I tried var_dump() on the two variables as well as on the results of intval() and strval() on both variables.
see code excerpt below:

Code: Select all

$imageX = $_POST['image1_X'];
$imageY = $_POST['image1_Y'];

var_dump($imageX);
var_dump($imageY);

$intX = intval($imageX);
$intY = intval($imageY);

var_dump($intX);
var_dump($intY);

$strX = strval($strX);
$strY = strval($strY);

var_dump($strX);
var_dump($strY);
The resulting text displayed on screen was as follows:

NULL NULL int(0) int(0) string(0) "" string(0) ""

That leads me to the conclusion that I must not be assigning the coordinate values to the variables correctly but then why then is the "page" variable stored correctly (referenced in first post).
Both variables are stored in the same way, as below:

Code: Select all

$page = $_POST['page'];
$imageX = $_POST['image1_X'];
But when I try to write the 2 variables to the file, only the page value is written.

I didn't find any special way to handle input image coordinates so I assume it's done that same way as any other POSTed value.

Any further suggestions would be greatly appreciated.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i'm not mistaken it are image_x and image_y that you're looking for... If you var_dump $_POST you'll see them ;)
karimD
Forum Newbie
Posts: 4
Joined: Fri Apr 07, 2006 5:42 pm

Problem Solved - the code is case sensitive

Post by karimD »

I just noticed I've been using an uppercase "X":

Code: Select all

$imageX = $_POST['image_X];
instead of a lowercase "x" :

Code: Select all

$imageX = $_POST['image_x'];
For anyone who comes across this thread in the future, the 2nd code snippet is the one that assigns the value correctly.

Thanks again for the quick response feyd.
karimD
Forum Newbie
Posts: 4
Joined: Fri Apr 07, 2006 5:42 pm

Post by karimD »

Thanks also to timvm!
Post Reply