2

I'm trying to open a page that let's the user know their form as been submitted in a pop up window that is centered on the screen. I have got the page to open in a pop up window but I can't figure out how to center the window with my current code (I was using a different js code that opened/centered the pop up but it also continued to open a new page with the same info).

Here is the code for the form currently...

 <form method="post" action="php/sendmail.php" target="POPUPW" onsubmit="POPUPW = window.open('about:blank','POPUPW','width=600,height=175');" id="contact">
    <h2>Name: (required)</h2><br />
            <input name="name" type="text" size="55" class="required error"><br />
            <h2>Email: (required)</h2><br  />
            </span><input name="email" type="text" size="55" class="required email"/><br />
            <h2>Phone:</h2><br />
            <input name="phone" type="text" size="55" /> <br />
            <h2>Comments:</h2><br />
            <textarea name="comments" rows="15" cols="47"></textarea><br />
            <br />
    <input type="submit" value="Submit" />
     </form>

The page is currently up at http://www.concept82.com/DIS/contact.html

I know this is probably really simple but I'm new javascript and php coding. Thanks so much!!!

  • I see that answer but cannot figure out how to get it to work in my case... – Kristin Shoffner Tanner Mar 22 '13 at 14:45
  • 1
    @KristinShoffnerTanner Popup windows are "passé" and many find them annoying. Plus many people will stop those with pop-up blockers or might have Javascript disabled altogether. If I may suggest that, you do a header redirect to a new page with a message saying that their message has been sent; it's much more professional ;-) Cheers – Funk Forty Niner Mar 22 '13 at 14:55
  • 1
    @Fred I agree with almost everything you said but I'd like to add that as well as potentially doing a header redirect to a new page with the message, which is the oldschool way of doing this, is fine, but I would much prefer to make an ajax call so I don't have to redirect and can achieve exactly what the OP wants. instead of a popup window or an iframe, just create a custom div styled to look like a popup and place the response from the php script in there – iAmClownShoe Mar 22 '13 at 15:14
  • 1
    @iAmClownShoe Agreeing there, for sure it's a good idea. There are many ways to do this, 'tis the beauty of having options, cheers :) – Funk Forty Niner Mar 22 '13 at 15:26

3 Answers3

1

Using your existing code in your question, have come up with the following.

Tested in FF 19.0.2 and IE 7

Changed pop.htm URL to suit and position, cheers.

var w = 200; // width in pixels
var h = 200; // height in pixels

<script>
function popupwindow(url, title, w, h) {
    var w = 200;
    var h = 200;
    var left = Number((screen.width/2)-(w/2));
    var tops = Number((screen.height/2)-(h/2));

window.open("pop.htm", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
}

</script>

<form method="post" action="php/sendmail.php" onsubmit="popupwindow()"; id="contact">
<h2>Name: (required)</h2><br />
        <input name="name" type="text" size="55" class="required error"><br />
        <h2>Email: (required)</h2><br  />
        </span><input name="email" type="text" size="55" class="required email"/><br />
        <h2>Phone:</h2><br />
        <input name="phone" type="text" size="55" /> <br />
        <h2>Comments:</h2><br />
        <textarea name="comments" rows="15" cols="47"></textarea><br />
        <br />
<input type="submit" value="Submit" />
</form>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • @iAmClownShoe what have I left out then? I believe I addressed her question, and I quote `"I'm trying to open a page that let's the user know their form as been submitted in a pop up window that is centered on the screen. I have got the page to open in a pop up window but I can't figure out how to center the window with my current code (I was using a different js code that opened/centered the pop up but it also continued to open a new page with the same info)."` All she needs to do is put the message inside `pop.htm` or whatever document name she wants it to be. – Funk Forty Niner Mar 22 '13 at 15:39
  • i just dont think OP knows what she really wants here. it's terrible practice to put a confirmation in a popup as most people run some type of ad/popup blocker and they would effectively never see her pop up. I think it's more important in this situation to not only address the simple styling of centering the element, but address the way in which she can go about achieving this in a way that mirrors common practices and will allow for her to insure that her users see the pop up. Your method of centering will absolutely work! i just wanted to show her how to make the server call the right way – iAmClownShoe Mar 22 '13 at 15:41
  • @iAmClownShoe I totally agree with what you're saying, I merely answered her question, stating that she is new to coding and helped her out with her existing code. I believe I fulfilled my end of the bargain ;-) – Funk Forty Niner Mar 22 '13 at 15:43
  • u did indeed sorry i retract my first comment as it is not true. you did answer the question. the problem is that the wrong question was asked :-P – iAmClownShoe Mar 22 '13 at 15:43
  • @iAmClownShoe I totally agree with you, 3 for 3 ;-) – Funk Forty Niner Mar 22 '13 at 15:44
  • would love to hear from OP about what it is she really needs to do here. if a page reload then the confirmation msg is not a problem then my answer is not useful to her. We aren't mind readers – iAmClownShoe Mar 22 '13 at 15:45
  • @iAmClownShoe No we're not mind readers, however we can make suggestions as well, and yours is a good one. She can use it as a `Learning Curve`. Will `upvote` your suggestion/answer. Cheers – Funk Forty Niner Mar 22 '13 at 15:46
  • just gave yours an upvote too as it does solve OP's actual question – iAmClownShoe Mar 22 '13 at 16:23
  • @iAmClownShoe - Thanks, much appreciated. She can take her pick. – Funk Forty Niner Mar 22 '13 at 16:25
  • @Fred Thanks for the help/advice...I have decided to do the old-fashioned way and send them to a new page (same header/footer as site) from there they are redirected back to the contact page – Kristin Shoffner Tanner Mar 22 '13 at 17:23
  • @KristinShoffnerTanner You're welcome. Maybe an `upvote` for my efforts? Cheers, glad you found a `solution`. – Funk Forty Niner Mar 22 '13 at 17:30
  • @Fred I tried but apparently my rep is not high enough yet...like I said I'm new to coding and this site. Sorry. – Kristin Shoffner Tanner Mar 22 '13 at 17:39
  • @KristinShoffnerTanner Not a problem. You can `accept` it if you want, if not that's ok. Am just glad you found a solution. Cheers – Funk Forty Niner Mar 22 '13 at 17:40
1

first of all, you should be using jquery to make an ajax call if that's the route you choose. The only way that you can post to your server side php script without a redirect is to do it asynchronously using Ajax. I HIGHLY recommend using jquery's ajax method for this as they have made it absurdly simple. To accomplish this you must save all your form input values to variables and then create a data object with them. That data object is then sent via the ajax call to the server.

<form method="" action="">
<h2>Name: (required)</h2><br />
        <input name="name" type="text" size="55" class="required error name"><br />
        <h2>Email: (required)</h2><br  />
        </span><input name="email" type="text" size="55" class="required email"/><br />
        <h2>Phone:</h2><br />
        <input name="phone" type="text" size="55" class="phone" /> <br />
        <h2>Comments:</h2><br />
        <textarea name="comments" rows="15" cols="47" class="comments"></textarea><br />
        <br />
<input type="submit" value="Submit" class="submitBtn" />
 </form>

Your html is slightly different in that i have changed the form by taking out the action and method as well i have added classes that i can use as selectors with jquery to your input elements.

$(document).ready(function(){
    $('.submitBtn').click(function(e){
        e.preventDefault();
        var name = $('.name').val();
        var email = $('.email').val();
        var phone = $('.phone').val();
        var comments = $('.comments').val();

        var dataObject = {
            name: name,
            email: email,
            phone: phone,
            comments: comments
        }

        $.Ajax({
        url: php/sendmail.php,
        type: 'post',
        dataType: "json",
        contentType: "application/json",
        data: dataObject,
        success: function(data){
                 var createdDiv = '<div class="divToBeCentered"><p class="dataText">' + data + '</p></div>';
                 $('body').append(createdDiv);
            };
        });
    });

});

in css to style the div to be centered you just need to do something like this.

.divToBeCentered{
    height: 100px;
    width:40%;
    margin:auto;
}
iAmClownShoe
  • 606
  • 1
  • 4
  • 10
-1

See this site will be help you Window open() Method

when you want to open the page in javascript change the

left=pixels
height=pixels
top=pixels
width=pixels

to your place

Kaiser Helawe
  • 43
  • 1
  • 5