1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

can you detect closing broswer and if submit was clicked?

Discussion in 'PHP' started by cl328, Jan 15, 2009.

  1. #1
    hi.

    i have a 3 part form:
    part 1 is 1st part of form (page 1)
    part 2 is the 2nd part of form (page 2)
    part 3 is the thank you page (page 3)

    so, once a visitor is on part 2 (page2) I want to FORCE or AUTOMATE the submit button on page 2.

    Obviously, i would like the visitor to fill out part 2 of the form and press the submit button -- but I can't count on that. (they are a user :) )

    the reason for all this:
    part 1 info is important.
    part 2 is not required, but I want it - if the user will give it too me.

    however, part 2 submit will complete the process.

    as of now, I save part 1 data into DB and IF part 2 is filled out (ie. visitor clicked page 2 submit) I delete the first record and just insert a new FULL record.

    If part 2 is never submitted (i have a cron job that completes the process)

    SO... i'm getting rid of the cron job process, etc.

    I need to FORCE the page 2 submit button WHEN:
    a) visitor closes browser
    b) visitor clicks any link on page 2

    AND obviously, DO NOTHING if the visitor will click the page 2 button by themself.

    I'm doing the processing via php.

    HELP!!!!!!!!!!!!!

    been dealing with this all day and not getting anywhere!

    i've looked into :
    javascript code like: window.onbeforeunload and the body onunload
    and the confirm function...

    nothing works as needed.

    How can i get this to work?

    MUCH appreciation for you help!!!!!!!!!!!
     
    cl328, Jan 15, 2009 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    Why don't you save yourself a lot of work and do it like this.

    Page 1 is required, so have all of the fields, and then have the action= go to page 2, using the POST method.

    Then, in the PHP for Page 2, have <input type="hidden" name="namehere" value="<?php $_POST['namehere']; ?>"/>

    that way, when they submit page 2, it will resubmit all of the forms for page 1 and page 2, with their names and values intact, and you wont have to worry about it if somebody just leaves in the middle of the form.
     
    crath, Jan 15, 2009 IP
  3. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi crath, that doesn't work.

    i have to save all the field data ONCE they submit one page 1, as you can ONLY save something after a user event (in this case, the submit on page 1)

    if i carry over the values from page 1 to page 2, via hidden fields, that is great, EXCEPT.... what is the user event that saves the page 2 value (and page 1 in the hidden fields) to the DB? if they don't hit 2nd submit, i lose everything then.

    I guess, i can make things required on page 2, but there has to be a way to get the functionality I want... right?

    that has to be a way?

    thanks
     
    cl328, Jan 15, 2009 IP
  4. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #4
    why don't you just combine page 1 and 2, and put a red * or something next to required fields?
     
    crath, Jan 15, 2009 IP
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    You can do that through database, but as said by crath, you should combine both pages other solution is kinda stupid but it can help you regarding this.

    After pressing submit button from first page, you pass mailer function on top of page 2 and receive all data in your email, so if they dont fill in the second page, you will have received the first page values, and then do same for the 2nd page. By using this method, you will receive two emails for 1 person.
    plus, you can also add hidden inputs of first few things on 2nd page so you can understand who sent these details.
    hope you understand.

    E.g;

    PAGE 1 - PAGE 2 - PAGE 3
    people submit page 1, you set mail function on page 2, and then, when people submit second page, you set mail function on 3rd page.
     
    khan11, Jan 15, 2009 IP
  6. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i split the form into 2 pages, due to length (so all fields on 1 page not good idea)

    the email idea is the same difference as writing to DB (whether its emailed or saved to DB) i still need a "trigger" / "event" ON the 2nd page.

    again, IF the user submits on the 2nd page -- all is good.

    my issue is, what if they don't (I will give notice to them they should, need to, must, etc) but a user is a user ---

    some how, some way... there MUST be a way to automate / force a submit button when the visitor leaves the page.

    here is my idea:

    on leave of page (however that is done) check to see if submit was pressed (however that is done), if pressed (do nothing, as that is the intended action), IF NOT PRESSED, programmatically,
    a) stop the page from leaving.
    b) execute the submit button.
    c) now when the page leaves, the button has been pressed and that is the goal.

    OR a different idea is to pop up a message telling the visitor to PRESS the submit button, to sort of create a loop, UNTIL the submit button is pressed.
    but that does not seem like a good idea :)

    so, 1 page form -- not the goal
    allowing user to not or forget to press button -- bad idea

    so isn't there a way to:
    1) check if submit button is pressed?
    2) stop a page from leaving (as in the only option, so I can force the submit)
    3) auto press the submit button

    thanks
     
    cl328, Jan 16, 2009 IP
  7. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #7
    my issue is, what if they don't

    i think you didn't understand my post.

    What i said was, when the user presses the first submit, it will direct them to page to as well as send the detail to your email. Then, if they don't submit the second button it doesnt matter.

    Put mail function above page 2 and execute it before people view the second page. got it?
    should i write some code to give you a start?
     
    khan11, Jan 16, 2009 IP
  8. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #8
    I'm still trying to figure out what you want, sorry I'm just not getting it.

    Page 1 = Fields that are required. Must be filled out, then when complete they hit Submit. That posts this info to a database, and directs to page 2.

    Page 2 = These are fields that are not required, but to complete the form, they still must click submit. If the user does click submit, all is well, but if they do NOT, and just leave, you have the data in the database that you dont want, and you want to remove it since they did not complete the form.


    you say
    but isn't that what you want to do? delete their records if they do not submit page 2?
     
    crath, Jan 16, 2009 IP
  9. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    okay... thinking here...

    1) i already save the page 1 data to DB, once they reach page 2. (yes, its at the top)
    2) when data is saved, i have a flag set to NO (as in 2nd page not complete ie. visitor is not done)
    3) if visitor never completes 2nd page, i have script that comes in and updates the flag to done. and all is good.
    4) if visitor does complete/submits 2nd page, i delete first record and insert new completed record. and all is good.

    trying to
    a) get rid of step 3
    b) not have multiple records (or emails) -- it a problem (human problem)


    so still, the best solution is to COMPLETE the record whether the user does it (pressed 2nd submit button)
    or the code does it via visitor leaving the 2nd page (other than the submit button)

    thanks
     
    cl328, Jan 16, 2009 IP
  10. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    crath --- i still want the record and its data, even if its only page 1.... Its just that, creating two records is bad (human problem)
     
    cl328, Jan 16, 2009 IP
  11. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #11
    So when they submit page 2, don't create a new record, update the existing one?
     
    crath, Jan 16, 2009 IP
  12. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    this can bring into the human problem....
    say, a person is already working on the record and then a few minutes later.. it gets updated -- that be bad.
     
    cl328, Jan 16, 2009 IP
  13. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    so is there a way to:
    1) check if submit button is pressed?
    2) stop a page from leaving (as in the only option, so I can force the submit)
    3) auto press the submit button
     
    cl328, Jan 16, 2009 IP
  14. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #14
    You can look into some javascript to accomplish some of the things you need, but I still believe you are going about this from the wrong direction.

    How would somebodys entry be updated between submitting page 1 and updating page 2. It would only be updating the fields that are being asked on page 2, nothing else.
     
    crath, Jan 16, 2009 IP
  15. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    a human starts to update the record (any or all fields)
    then a time later, the visitor completes the form and over writes the fields.

    i understand the likelyhood of this happening is not often, however, that is not the point. it would happen.

    i have looked into the javascript stuff... and either it can't work,
    or i found the wrong code, or the functionality is not quite the same
    but -- i'm not javascript expert

    i was hoping / thinking PHP could do it?

    or some combination
     
    cl328, Jan 16, 2009 IP
  16. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    HERE is different take.

    when visitor leaves 2nd page, if submit button NOT pressed, update flag on the record to complete????

    so, how to execute php ONLY when leaving page AND submit not pressed?
     
    cl328, Jan 16, 2009 IP
  17. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    php can't detect when page is leaving -- correct?

    so, that leaves javascript to detect page is leaving... which comes back to the whole thread :(
     
    cl328, Jan 16, 2009 IP
  18. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #18
    well... this is a horrible way to do things, but try something like this

    add a name= tag to your <form element, and then replace "myform" below with the name of your form.

    This will submit the form whenever the window is closed.

    <body onunload="document.myform.submit();">
    Code (markup):
    I don't even think that will work, because the window is already closing.
     
    crath, Jan 16, 2009 IP
  19. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    oh... didn't know that is how you auto submit the form.

    okay... i need to know if the submit was already pressed? can you figure that out?

    so, onunload, i call function.
    the function checks if the submit has been clicked.
    YES -- do nothing
    NO -- do this document.myform.submit()

    that should work?

    as llong as i can determin is butotn has been pushed?

    thanks
     
    cl328, Jan 16, 2009 IP
  20. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    PS. actually, i have some ajax code that is called by javascript that will execute PHP script... so at least, i should be able to update the flag in the record to complete

    uhmmmmmmmmmmm
     
    cl328, Jan 16, 2009 IP