Forum Moderators: open

Message Too Old, No Replies

Rewriting form action and target with OnClick

.... at the same time. My code not working.

         

trillianjedi

11:02 am on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm stuck!

I'm trying to re-write document.mainform.action within an OnClick event tied to a graphic image "button" and display the results in a popup window.

So I have a normal button on the page which, as you would expect, posts the data to the "action" URL of the form.

When the graphic button is clicked, I switch the "action" to a different script, and try to display the results in a popup window:-

<form name="mainform" action="normalsubmit.php" method="post">

<input type="image" src="pushme.jpg" name="action" value="PushTheButton"
onClick="document.mainform.action='TheOtherSubmit.php'";
document.mainform.target="foobar:width={350}, height={150}, scrollbars, {(isResizable)?'resizable':''},status";
document.mainform.onSubmit="return createTarget(this)";
document.mainform.submit();>

The function I'm using to set the popup dimensions is:-

<!-- Begin
// test variables
var myHeight = 200;
var isResizable = true;

function createTarget(form) {
_target = form.target;
_colon = _target.indexOf(":");
if(_colon!= -1) {
form.target = _target.substring(0,_colon);
form.args = _target.substring(_colon+1);
} else if(typeof(form.args)=="undefined") {
form.args = "";
}
if(form.args.indexOf("{")!=-1) {
_args = form.args.split("{");
form.args = _args[0];
for(var i = 1; i < _args.length;i++) {
_args[i] = _args[i].split("}");
form.args += eval(_args[i][0]) + _args[i][1];
}
}
form.args = form.args.replace(/ /g,"");
_win = window.open('',form.target,form.args);
if(typeof(focus)=="function")
_win.focus();
return true;
}
// End -->

The form action target seems to be set correctly and does call the other script, so that part works fine, but I can't seem to get the popup part to work (the result is displayed in the same page instead).

I can get the popup to work fine in isolation (i.e. if I don't do the form.action switching part).

Is it not possible to have both of these actions incorporated into the OnClick event?

Thanks all!

TJ

Bernard Marx

4:29 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is one complex script there, trillianjedi.

Firstly, there is some highly erroneous quoting going on in that inline onClick handler.
I'd advise putting all that into a function, and calling it.

I haven't had the chance to see what you're doing. I suspect there's a simpler way. Just can't remember how to do it yet.

Bernard Marx

4:57 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html>
<script type="text/javascript">

function popSubmit(form)
{
var popName = "formPopUp";
var popStyle = "width=300,height=300,location=yes,resizable=yes";
form.action = "theOtherSubmit.php";
form.target = popName;
window.open("about:blank",popName,popStyle);
}
</script>
<head>
<title>Table Row CSS Visibity Test</title>

<form name="form1"
method="get"
action="normalSubmit.php">
<input name="text1" type="text" value="some text"><br>
<!-- normal -->
<input type="submit" value="submit"><br>
<!-- popup -->
<input type="image" src="someImage.gif"
width="100" height="100"
onclick="popSubmit(this.form);">
</form>

</body>
</html>

trillianjedi

7:23 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bernard - that's brilliant - thanks. I can get your code to work perfectly.

But I'm still a little confused as I can't seem to get my code to work.

Here's the page source:-


<html>
<script type="text/javascript">
function popSubmit(form)
{
var popName = "formPopUp";
var popStyle = "width=300,height=300,location=yes,resizable=yes";
form.action = "MySuperForm.php";
form.target = popName;
window.open("about:blank",popName,popStyle);
}
</script>
<head>
<title>TJ Test</title>

<form name="form1"
method="get"
action="normalSubmit.php">

<whole load of form objects here>

<!-- popup -->
<input type="image" src="click.jpg" name="action" value="MySuperForm"
onclick="popSubmit(this.form);">
</form>
</body>
</html>

... and then in MySuperForm.php, I'm doing this:-

// Ok now let's see what they wanted
switch ($_REQUEST['action']) {

// Seems they posted to me so let's process it
case 'MySuperForm':

To check when the new graphic button is clicked.

Doesn't seem to register. I'm convinced I'm doing something stupid here.... sorry, but I just can't see what it is.

Many thanks,

TJ

Bernard Marx

7:51 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haven't run this yet, but I'll bet a decent cup of tea on it being

<input name="action" ...>

Because of the "flat" nature of the DOM still used in forms wrt naming, this conflicts with the form's own

action
attribute.

Try it with a different name.

--------------------------------------
(Just out of interest...)
Before changeing the name of the input, you could also run it just once more, and do a:

print_r($_REQUEST);

to see what, if anything, is being received.

Bernard Marx

8:32 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seems like it's not that (but you still can't have my tea).

I favoured using <input type="image"> because at least the form could still be submitted if JS-disabled. However, since in this case it would mean it being submitted to the wrong script, it seems a bit of a faulty fallback.

Image-submit buttons, when named, appear to send the x and y position values of the click event (from there own reference frame). In a query string they look like:

?action.x=23&action.y=35

In PHP, for both GET and POST, the dots are replaced by underscores. Thus there is no

[blue]action[/blue]
$_REQUEST attribute, but
[blue]action_x[/blue]
and
[blue]action_y[/blue]
, and the values aren't from the value HTML attribute.

A solution would be to change the image-submit element for something else (as you had before). Everything remains the same, except you'll need to add

[blue]form.submit()[/blue]
as the last line of the function.

I would still recommend using some other name than "action".
Let's call it "actionType".

It seems there are 2 options for getting our value for actionType passed over, whilst using an image for the submission element.

1) Use a second submit button, name="actionType" value="MySuperForm".
Then restyle the button to llok like an image.

2) Use an image (perhaps wrapped in a link).
You'll then need a: <input type="hidden" name="actionType" value="normalSubmit">
The function will have to change the value of that before submitting.

coopster

9:45 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Spot on with how PHP handles the HTML [php.net] <input type="image"> Bernard_Marx.

However, it is not PHP that fudges the name=value pairs coming back, but the oh-so-wonderful MSIE browser. Moz-based browsers will indeed send all three values back [webmasterworld.com], the name, the name_x coordinate and the name_y coordinate. Therefore, best practice is to check for the existence of one of the coordinates in the $_POST superglobal, that way you will know that it was indeed clicked.

<?php 
if (isset($_POST['name_x'])) {
...
?>

Now you really don't need to know the value so you wouldn't have to set a value. Just code to check for the button click as described earlier.

Bernard Marx

11:26 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but the oh-so-wonderful MSIE browser.

Typical!

I was thinking of suggesting a check for a name_x variable - it's certainly easier, but since the actual value wasn't being transmitted, I started to think of the (unlikely) eventuality where there might be more than one image submits with name="action". For IE users, we wouldn't know which one was clicked

(...but we would know which pixel! Daft.
So we'd have to combine the images into one, and use the x,y position to decide meaning)

DrDoc

11:30 pm on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you would know which image was clicked.

<input type="image" name="foo"> -> foo_x
<input type="image" name="bar"> -> bar_x

<added>
Opera doesn't send the plain name value either.
</added>

Bernard Marx

12:21 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tru, Doc, if the name was being used in that sense, but the name attribute was being used differently in TJ's code (#4)

<input type="image" src="click.jpg" name="action" value="MySuperForm"
onclick="popSubmit(this.form);">

The actual identifier is the value attribute. If we have 2 of these, with

value
s signifying which button is clicked, then the info is lost.

(..but we don't have 2, so I'll be quiet now)

DrDoc

12:24 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:)

Yes, you are absolutely right. Not getting the value (as a plain name=value pair) from an image submit button is, quite frankly, ridiculous.

coopster

12:37 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




<added>
Opera doesn't send the plain name value either.
</added>

I was wondering about that and just never took the time to test it in Opera. Yes, too lazy ;) Thanks for heads up, DrDoc.

trillianjedi

10:29 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys - this all makes perfect sense to me (if not the MSIE foibles, at least the work-around).

But I'm further confused as I can't seem to get the thing to work. This is now officially a PHP problem for me, I might need to switch forum, but while we're here:-

if (isset($_POST['foo_x'])) {
echo "Foo was clicked";
}

Does not execute, even though the preceding line:-

print_r($_REQUEST);

Shows quite clearly it is:-

[foo_x] => 37 [foo_y] => 16

Am I missing something...?

Thanks for all the help so far - I'm sure it's so close I can smell it, but there must be something that I'm still doing wrong....

TJ

DrDoc

12:22 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm assuming your form method is set to post?

$_REQUEST['foo_x'] would work otherwise, but that doesn't really solve the problem :)
What does print_r($_POST) give you?

trillianjedi

12:35 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm assuming your form method is set to post?

D'OH!

Thanks doc ;)

trillianjedi

1:07 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, excellent, all up and running - many thanks.

Final JavaScript question, is it possible to do this:-

FILE: tj.js

<script type="text/javascript">
function popSubmit(form)
{
var popName = "formPopUp";
var popStyle = "width=305,height=160,location=no,resizable=no,scrollbars=no,toolbar=no";
form.action = "MySuperScript.php";
form.target = popName;
window.open("TJ Test",popName,popStyle);
}

document.write('<input type="image" src="click.jpg" name="MySuperScript" onclick="popSubmit(this.form);">');

</script>

So that I can then insert this "new" button into any webpage form simply with a:-

<script src="tj.js"></script>

Or is that pushing my luck?

Thanks,

TJ

DrDoc

1:14 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Close.

First, remove the script tags from your tj.js
Second, add the type attribute to the script tag calling your tj.js

Good to go :)

trillianjedi

2:27 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Awesome!

Bernard Marx

5:29 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about the GET, TJ. I think that was me. I did some tests with PHP (GET & POST), but at some point I wanted to see what was appearing in the address bar. Didn't change it back.

trillianjedi

6:47 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem Bernard - thanks for all your help - you've been a star ;-)