最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how i can close popup window in php ...? - Stack Overflow

programmeradmin5浏览0评论

i have a page in where the different button available. on each button when a user click a popup window open. e.g user click on ADD button and a popup window appear. user put some data in controls and press on save button. i want to close the open popup when user press on save and and data save successfully. i m using this code to close the form

function closeForm() {
    echo "<script language=\"JavaScript\">\n";
    echo "function closeForm(){ \n";
    echo "window.self.close();";
    echo " } \n";
    echo "</script>\n";
    return true;
} 

this function call if the data successfully save.

any one help me to fix this problem...?

i have a page in where the different button available. on each button when a user click a popup window open. e.g user click on ADD button and a popup window appear. user put some data in controls and press on save button. i want to close the open popup when user press on save and and data save successfully. i m using this code to close the form

function closeForm() {
    echo "<script language=\"JavaScript\">\n";
    echo "function closeForm(){ \n";
    echo "window.self.close();";
    echo " } \n";
    echo "</script>\n";
    return true;
} 

this function call if the data successfully save.

any one help me to fix this problem...?

Share Improve this question edited Apr 20, 2011 at 16:16 Mike Thomsen 37.5k11 gold badges63 silver badges86 bronze badges asked Apr 20, 2011 at 16:15 saintsaint 3,9155 gold badges21 silver badges17 bronze badges 3
  • you seem to already know that javascript is the way to close the window. why do this roundabout thing thru PHP? you can set the onclick action to a javascript function that first sends the data to server/PHP (probably ajax) and then closes the window – jon_darkstar Commented Apr 20, 2011 at 16:35
  • 2 You have to remember that PHP on the server and javascript in the browser are pletely separate. You could submit the new information using ajax (making a call to your PHP) and then use the response in a javascript callback to close the popup? – Ken Commented Apr 20, 2011 at 16:38
  • @mofle : please explain. y this wrong....? i already use click handler for some validation process. some business rules apply when user click on save button. – saint Commented Apr 21, 2011 at 8:17
Add a ment  | 

5 Answers 5

Reset to default 2

There is no way to do it in PHP, use JavaScript instead. That's how your button should be coded in HTML:

<input type='button' value='Close' onclick='javascript: window.close();' />

Sir,

Your question is very vague, possibly mis-categorized and difficult to understand.

That being said, I will attempt to answer you, although, Algorithm first:

  1. You have a workflow process where users are entering data.
  2. You are guiding them through a series of steps, in which you are gathering different bits of information at each step.
  3. You need to capture the data at each of these steps from a "form" (HTML form, or Javascript Form) and then you need to successfully save the captured data.
  4. You need to supply the user with feedback on whether the operation was successful or not.
  5. After the form successfully saves You need to direct the user to another area.

Just to be crystal clear PHP is a server-side technology that has the ability to send dynamically generated HTML pages to a client's web browser, such as an HTML form, and to process the result of an HTML form that was "posted" back to it.

PHP also has the capability to do a SQL database "INSERT" to store your information, and then to subsequently evaluate the result of the database "INSERT" query.

The idea implied here is the user requests a page that contains a form where they can input data, (this form can just be one step in a series of steps...) the user then inputs their data and clicks the "next" or "submit" button in the form and their browser captures the input and sends it back to the server.

The server receives this information on a specific page (specified in the HTML form attribute "action") and has access to the sent data in one of several "super-global" PHP arrays ($_REQUEST[], $_GET[] or $_POST[], which is usually "$_POST" and is specified in the HTML form attribute "method").

The PHP script can then process the input: checking for XSS (cross site scripting), SQL Injection attacks, invalid input, etc. and if the input is valid execute a database insert query. After evaluating the result of that query and send an HTML "success/fail" page response back to the original user, or send the user the next HTML page (the next step in the process).

Javascript (like the code you supplied us with) is a client-side technology (client side ONLY!!!) that executes only in the end users web browser. Using a javascript form to capture user input (as in being able to hold it inside of persistent storage, like a database) defeats the purpose and usefulness of the well-known and "well-lit" path of using HTML forms to POST back to a PHP script/file/method... UNLESS you are talking about AJAX calls.

AJAX (Asynchronous JAvascript and XML) is not a new technology or another programming language rather it is using Javascript to make a "behind the scenes" call (on an independent "thread" on the client puter) to a specified PHP script/file/method to get a specific answer and then to insert the answer dynamically back into the current HTML DOM. (partial-page refresh, whole page DOES NOT refresh.) Then using Javascript forms to capture input could be interesting... although somewhat "off the beaten path", due to the fact that the user must have javascript enabled in their web browser in order for your site to function properly.

The code that you supplied us with indicates you are using javascript to close the entire web browser window, I'm confused...?

I would suggest investigating using Javascript/jQuery to change the "CSS visibility" attribute of an embedded HTML form, bined with some AJAX calls back to the server, instead of a pop-up of some kind, that way you could still use your "click button and input area appears" method, while still having a solid back-end infrastructure in place to handle input, processing and response.

Best of luck! :)

I beg your pardon but there is a way to do this in PHP via JS:

echo "<script type=\"text/javascript\" charset=\"utf-8\">window.self.close()</script>";

You can do this once all your PHP serverside processing is done.

Set it as the click handler for the button.

Plain way:

<input id="save_button" type="button" onclick="closeForm();" value="Save"/>

jQuery way:

$("#save_button").click(closeForm);

Where you added the button of success just add a data-dismiss="modal" as a attribute like this:

'''

    <button type="button" class="btn btn-primary" onclick="saveNotes()" data- 
    dismiss="modal">Save changes</button>

'''

发布评论

评论列表(0)

  1. 暂无评论