Form button that executes PHP code and closes window

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
motleycrewl
Forum Newbie
Posts: 8
Joined: Tue Mar 04, 2008 2:16 pm

Form button that executes PHP code and closes window

Post by motleycrewl »

I'm trying to get a form button to execute simple PHP code that places some data into a database. (So far this works fine: if ($_POST['submitted']) { ...code...})
Once that's done I need the pop-up window to close and have the parent window refresh to display the newly inputted data.
I know i need JavaScript but no matter what i do I cant seem to get all three of these function to work in unison. Anyone have any ideas? Im fairly new to Javascript. 8O
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Form button that executes PHP code and closes window

Post by hansford »

Not sure what you're trying to do - ie the popup window-whats that for? This can all be done on one page. here is a basic example.

Code: Select all

//first we check if the form has been submitted
---------------------------------------------------
if(isset($_POST['action'])){
  //if so then we can write out some javascript for whatever reason
  //connect to database and display infomation
  echo "<script language='javascript'>";
  echo "var name ='" . $_POST['name'] . "';";
  echo "</script>";
}
else{
  //if no then we write out form which will submit page to this same page
  echo "<form name='form1' action='$_SERVER[PHP_SELF]' method='post>";
  echo "<input type='text' name='name'/><br>";
  echo "<input type='submit' value='submit' name='action'></form>";
}
}
Last edited by RobertGonzalez on Mon Jun 02, 2008 11:09 pm, edited 1 time in total.
Reason: Please use bbCode tags when posting code in the forum
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Form button that executes PHP code and closes window

Post by RobertGonzalez »

PHP will do what you need it to on the server. Javascript will need to be added to the output so that when the page loads the window closes (or something akin to that).
Post Reply