...

View Full Version : 3 buttons in a form



briintex1
12-23-2003, 06:18 AM
:confused: I have 3 pictures that are on a form on a HTML page, and I use the post so when I post it will goto a php page...but my problem is that when I do ask it to post it does not transfere it to a php page, or not that I know of...is there a way that I c an do this...or that I can set the name of the buttons on the HTML page, to a variable on the PHP page, if I can then I can use functions and do what I want to on the page, and not have to make 3 pages..I have also been asked not to use any javascript.

Celtboy
12-23-2003, 06:33 AM
While I'm sure there's a much prettier way of doing things, I usually use a combination of javascript and php for this...how?



<script language="JavaScript">
<!--
function whichButton(theButton) {
document.formname.buttonSet.value = theButton
document..formname.submit()
}
//-->
</script>


Then in your form, make a

<input type="hidden" name="buttonSet">

in the buttons,


<input type="button" value="Option A"onclick="whichButton(this.value)">

That could be wrong, and I'm sure it isn't cross-browser compatible.

Then in your php file,


...
switch ($_POST["buttonSet"]) {
case "Option A" :
.....
break;
default :
...
}

That's a really rough idea of one consideration.

Celtboy
12-23-2003, 07:50 AM
Another possible solution:

1. Ignore the javascript entirely.
2. Do the following:



<input type="image" name="image1" value="1" src="yourimage.jpg">
<input type="image" name="image2" value="1" src="yourimage2.jpg">
<input type="image" name="image3" value="1" src="yourimage3.jpg">


Then in your php code, just check for variable existence!



while ($found != TRUE) {
if (!isset($_POST["image1_x"])) {
$found = false;
} else {
$found = TRUE;
$submitted = "image1";
}
if (!isset($_POST["image2_x"])) {
$found = false;
} else {
$found = TRUE;
$submitted = "image2";
}
if (!isset($_POST["image3_x"])) {
$found = false;
} else {
$found = TRUE;
$submitted = "image3";
}


}



Because the variable names aren't passed, we get what we can...the variablename followed by an underscore and "X" or "Y" for the coordinates.

raf
12-23-2003, 04:28 PM
give the three the same name (for instance image) in your form, but a different value (1, 2, 3).

then,inside the php,you get the clicked image with


$selected = $_POST['image']

briintex1
12-23-2003, 05:49 PM
this is on the index.html page

<input type="image" name="test" value="1" src="Something.jpg" alt="Something">
<input type="image" name="test" value="2" src="This.jpg" alt="test"></td>
<input type="image" name="test" value="3" src="That.jpg" alt="That"></td>

this is on the pictures and names on the php page

<?php
$selected = $_GET['name']
?>


<?php
echo($selected);
?>
I did what you said, but now I get an error, and it says Undefined index: image in Folder on line 3

raf
12-23-2003, 08:05 PM
A few question:

-why do you search for the variable in the qurystring collection, while you specify in your first post that you use the post-method?
- what are you trying to do? do you want to use the images as submitbuttons (so you can also post the values from other formfields)? Or do you just want to create a link on the images?

briintex1
12-23-2003, 09:22 PM
Well, I think what I messed up was the question, maybe?

First I have a form that is on a html page, with three pictures on it, that are called different names. Well with a click on an image, it sends the name of the image to a php page. On the php page I want it to call a function that I have written for the certain images -(that way I do not have to have 3 PHP pages)- that was pushed on the html page. That is the reason that I was wondering if there was a way to do it. The only way I figured it could be done was with a form and a post, so it would send the name of the image to the php page, and display what I wanted it to in the function because I would call the function with PHP. I am not sure if that is the answer that you are looking for, but that is where my problem does lie, on how to call the name of the image from a HTML page, and send it to the PHP page, and call the function in the PHP page. If there was a way to set the name of the image to a variable on the PHP page, then I could end up doing it?

raf
12-23-2003, 10:51 PM
If i understand it correctly (i'm not the smartest one arround:( ) then you just wan't to know which image was clicked. Right ?

Now, you don't need a form for this. You can just lay a link on the image, that points to your php pahe (lets say page.php). Then you add a variable-value pair to the querystring. Like:


<a href="page.php?picture=1" title="Get the pictureaction by clicking on it!"><input type="image" src="Something.jpg" alt="Something" /></a>
<a href="page.php?picture=2" title="Get the pictureaction by clicking on it!"><input type="image" src="Something2.jpg" alt="Something2" /></a>
<a href="page.php?picture=3" title="Get the pictureaction by clicking on it!"><input type="image" src="Something3.jpg" alt="Something3" /></a>

and then, inside page.php:
<?php
$selected = $_GET['picture']
?>

You see ?
You only need the formconstruction if you want to also get formfiel-values to page.php, but this will probably envolve a submit with an onclick javascript event.

briintex1
12-24-2003, 07:08 AM
That did it, I had to play with just a little bit to get the link to work but that did what I wanted to do...now I am workin' with functions haha...just like C++ I think..thank you for your help I do appreciate it, thanks again:thumbsup:

raf
12-24-2003, 08:56 AM
you're welcome. Glad you got it running :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum