Results 1 to 8 of 8

Thread: problem with submit

  1. #1
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy problem with submit

    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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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>
    Last edited by jscheuer1; 11-03-2007 at 09:14 AM. Reason: improve code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much that really did it.



    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.


  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't follow completely, but I get the general idea. First off, the correct way to do:

    Code:
    document.form2.submit()
    would actually be:

    Code:
    document.forms['form2'].submit()
    or:

    Code:
    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;"
    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    John thank you so much for all your help

    I totally appreciate it

    thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •