View Full Version : problem with submit
khaled450
11-03-2007, 06:41 AM
hi there, i'm new here, and I am really seeking help.
I was trying to make a form where you I can click on a picture to submit. To make it complicated for myself, I wanted it pass a variable in a hidden field and also does an onclick script. So the code is similar to:
<form action="1.php" method="post">
<input name='2' hidden value="<? echo $x;?>" />
<a href="#" onClick="openPopUp()"><img src="img.gif"/></a>
</form>
So once I clicked on that picture, a pop-up window(1.php) appears in there, with the value of input name'2'
1.php has:
$y= $_POST['2'];
echo $y;
I hope somebody will help me with this.
jscheuer1
11-03-2007, 07:11 AM
Unrelated to your question, you shouldn't use a number for a name. Just change it in both places to something like - 'a_2' or whatever. Something descriptive of its purpose would be best, like if it were a color, you could name it 'color' or 'color_2'. Numbers used as names or id's are non-standard and can confuse browsers and servers alike.
Now onto your question - it's hard for me to say exactly because I am both somewhat unfamiliar with what happens when you open a window via javascript containing the page that your form would normally open in the current window (it can be done, but it probably wouldn't receive the post data), but something like so may work out:
<form action="1.php" method="post" target="_blank">
<input name="color_2" type="hidden" value="<? echo $x;?>">
<input type="image" onclick="this.form.submit();" src="img.gif" alt="Submit" title="Submit">
</form>
jscheuer1
11-03-2007, 08:11 AM
I researched this a bit more and found some answers that didn't work too well, but that gave me the idea for this solution which seems pretty good:
<form action="1.php" method="post" target="post_win">
<input type="hidden" name="color_2" value="<? echo $x;?>">
<input type="image" src="img.gif" onclick="var f=this.form;
window.open('',f.target,'width=300, height=200, location, resizable');
setTimeout(function(){f.submit();},100);return false;"
alt="Submit" title="Submit">
</form>
khaled450
11-03-2007, 10:56 PM
Thank you so much that really did it.
:D
And I will concider not giving names such numbers in the future, thank you so much, I didnt know about the confusion thing.
Another thing, what if I had (p1.php, p2.php, p3.php), and p1.php has:
$x=3;
<form action="p2.php" method="post">
<input name="first" value="<? echo $x;?>" />
<input type="submit" />
</form>
.......end of p1.php........
p2.php
$y=$_POST['first'];
<form name="form2" action="p3.php" method="post">
<input name="scd" TYPE="HIDDEN" value="<?echo $y;?>" />
<a href="#" onClick="doSth()">SEND ME</a>
</form>
*********
I've seen that writing something like this "document.form2.submit()" in onClick would make the form to submit, but not sure if that's true;however, in my case onClick has a value to open the pop-up window, so i can't include anything in there.
*******
..........end of p2.php
p3.php
echo $_POST['scd'];
.
.
.
etc
.....end of p3.php
Thank you so much again, and I hope I'm not confusing everybody here.
:D
jscheuer1
11-04-2007, 04:07 AM
I don't follow completely, but I get the general idea. First off, the correct way to do:
document.form2.submit()
would actually be:
document.forms['form2'].submit()
or:
document.forms.form2.submit()
The forms collection can usually be assumed, but if there is any other object on the page that could possibly be thought to be 'form2', there will be problems. Or a reference could be created for the form in some other unambiguous manner as a variable, as I did in my code. then the form may be referenced via that variable.
Now, let me break down how my previous code works, it may help answer your current question.
onclick="var f=this.form;
window.open('',f.target,'width=300, height=200, location, resizable');
setTimeout(function(){f.submit();},100);return false;"
var f=this.form
simply establishes a reference to the current form, but only in the scope of the current onclick event, thus protecting it from any other var f on the page.
window.open('',f.target,'width=300, height=200, location, resizable');
The window.open method is opening an empty ('') window and naming it (f.target) after the target attribute of the form. The rest are just the specifications for the window, which can be configured however one likes.
setTimeout(function(){f.submit();},100);"
Now that we've given the the window a chance to open, after 100 milliseconds, submit the form. The form's target is the newly opened window's name, so its action (complete with any method data) opens in the already opened but empty window.
return false;
By returning false we ensure that there will be no automatic submission (which would happen too soon for the window we are opening) of the form, as can happen when the default (in this case the only visible) input of a form is activated.
khaled450
11-04-2007, 08:12 AM
Thank you sooooooooo much ... breaking to pieces made me understand the functionality of it and how to play with it.
My previous post was about something different than my first. I was wondering about how can I make a href act as a submit button "if that's possible". However, the onClick is busy that it does some cheaking first (onClick="validate()").
...
My other question, is it better to use $_SESSION to pass variables around or get and post. I'm asking this because i'm using post now and everytime i refresh the page i submit my form to, i get this WORNING message from IE asking if i want to resubmit. Is it possible to use many sessions at a time?
Sorry for all the trouble but i'm just learning from thr pros here.
Thank you again :o
jscheuer1
11-04-2007, 09:48 AM
I really don't know that much about the server side, you could ask in the PHP forum, as I see that is the server language you seem to have at your disposal.
The onclick event that I assinged to the image input could be on a link, but it makes little difference, you can include your validation function as the first thing in the image input's onclick event, returning false if it fails. If you use a link though, you can no longer reference the form as 'this.form'. You can use the forms collection though as mentioned in my previous post.
Using the input with a validateFunction() that returns true or false using the form as its parameter:
<form action="1.php" method="post" target="post_win">
<input type="hidden" name="color_2" value="<? echo $x;?>">
<input type="image" src="img.gif" onclick="var f=this.form;
if(!validateFunction(f)) {return false};
window.open('',f.target,'width=300, height=200, location, resizable');
setTimeout(function(){f.submit();},100);return false;"
alt="Submit" title="Submit">
</form>
khaled450
11-07-2007, 07:50 AM
John thank you so much for all your help
I totally appreciate it
thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.