Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form 2

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
Hi Any help appreciated.

When the page loads the first thing that happens is a query is run to see if the last time sheet was completed correctly ie each time field has a value.

If it is incomplete a session variable is set with the last entries date. The user can then make any adjustments or add extra times.

Now the issue I have is this. The incomplete time sheet is displayed in a form and I would like to have two options, if save is selected, then the page is submitted to itself and a query is run to update / insert the new values and the same page / form appears. The other option is the user fills in all the fields that are outstanding and instead of clicking on the save button clicks on a complete button. What I would like it to do is save the details and remove the session, but how can I tell it to do this?

Here is a basic version of what I have. Is it possible to have effectively have two submits buttons for the same form and use it with php?

Any help appreciated. Thank you.

<?php
if($_POST['submitted']){

//update query

}
?>
<form action="test.php" method="post">
Time 1<input type="text" name="time1" /><br /><br />
Time 2<input type="text" name="time2" /><br /><br />
Time 3<input type="text" name="time3" /><br /><br />
Time 4<input type="text" name="time4" /><br /><br />
Time 5<input type="text" name="time5" /><br /><br />
Time 6<input type="text" name="time6" /><br /><br />
<input type="hidden" name="submitted" value="true" />

<input type="image" src="/images/save.png" /><br /><br />
<input type="image" src="/images/complete.png" /><br /><br />

</form>
 
Should there be values to evaluate when the form is processed?
 
Hi spamjim

Yes there are values that are pulled from the database ... I did not want to put to much info into complicate the matter. In a nut shell is it possible to have two buttons in one form? one does one thing and the other another thing. Sorry if I am not very clear in explaining.
 
Yes its perfectly possible.

Just name your 2 buttons to uniquely identify them and test fro their prescence.

Only the pressed submit button gets sent with the form.

Code:
<input type="image" src="/images/save.png" [red]name="save"[/red]/><br /><br />
<input type="image" src="/images/complete.png" [red]name="complete"[/red] /><br /><br />

Image buttons send the coordinates of where the image was clicked into the $_POST variuable such as buttonname_coord, (button_x, or button_y) So just test for the presence of one of those so you know what code to run.


Code:
if(isset($_POST['[COLOR=#A40000]save_x[/color]'])
{
  //Do save things
}
elseif(isset($_POST['[COLOR=#A40000]complete_x[/color]'])
{
  //Do complete things
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
My comment about values was related to the ambiguous nature of your two image inputs. They needed distinction so that the form processing script could tell which image was clicked, as vacunita illustrated well.
 
@Phil
is an image button submitted to a server if the form is submitted programmatically or by pressing return? If not I guess the OP should decide on a default action rather than use an elseif. the alternative would be to embed a hidden field in the form and then have the non-default action given the same name as a button. the latter would override the former.


 
Good point.

If you submit the form using Js by calling the form's submit() method, then yes, non of the buttons will get sent because you aren't actually pressing a button, or even using a button's click() method. .

If you use the click() method for either one of the buttons then that button gets sent.

In any case, this being the PHP forum, and as I see no mention of JS submissions I did not consider that.

However, yes, you would need to consider that neither of the buttons would be sent if there were JS submissions involved that used the forms submit() method.

[2thumbsup]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
what happens if you just click 'return/enter'? I should know, really...

does the browser assume that the earliest button (with submit type) in the DOM is the one that is clicked? in which case I wonder (for an image button) what coords would be sent? I'll go test this evening...
 
Interestingly enough, yes the first button in the DOM was the one that got submitted, and the coordinates, where actually quite strange:

button1_x: -188
button1_y: -104

All this was tested using Chrome and FF. IE might do something different, but as I'm on Ubuntu at the moment, and haven't had time to recreate my Windows Virtual Box yet, I can't test on IE.

I'll test again tonight at home on my actual Win8 box.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
[not to hijack this thread but ...] i'm quite keen on the new ubuntu phone - the edge. really hoping they get enough money to get this running. Sappphire screen ... drooooool.
 
This topic is getting heavy. Couldn't one just avoid the image input type altogether? Aside from the original processing issues, the form is not quite accessible with the image input type.

A submit input type could be styled with an image. Even if the images fail to load or the CSS is not applied, a good old submit button is still available.

The CSS in this example page puts an image in the background of a button and offsets the default button text so it is unseen.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Button Demo</title>
		<style type="text/css" media="screen">
		<!--
		.buttonyes{ background: url('[URL unfurl="true"]http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/32x32/status/dialog-clean.png')[/URL] no-repeat top left; width:32px; height:32px; border:0px; text-indent:-1000em; }
		.buttonno{ background: url('[URL unfurl="true"]http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/32x32/status/dialog-error-5.png')[/URL] no-repeat top left; width:32px; height:32px; border:0px; text-indent:-1000em; }
		-->
        </style>
	</head>
	<body>

	<?php
		if ($_POST['do']=="it") { print ("You chose YES."); }
		if ($_POST['do']=="not") { print ("You chose NO."); }
	?>
	
		<form id="demo" action="index.php" method="post" name="demo">
			<input type="submit" class="buttonyes" name="do" value="it" />
			<input type="submit" class="buttonno" name="do" value="not" />
		</form>
	</body>

</html>
 
...and regarding which button is submitted by default on the press of the "enter" key... it depends on the browser and the version. Typically, the first button in the DOM is passed. But IE8 and earlier does not appear to pass on either button if it is not explicitly clicked. A way to avoid the mess is to disable the "enter" key. This can go in the HTML head to kill "enter":

HTML:
 <script type="text/javascript">
    function stopEnterKey(evt) {
        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
    }
    document.onkeypress = stopEnterKey;
</script>
 
Well, there's really no difference between a submit button and an image button in terms functionality, and JS behavior.

At the end of the day, you still have to check what got submitted, and if submitting via JS, deal with the possibility than none may get submitted.



*Tangent*************** The Ubuntu phone looks really cool.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top