okrobie Posted January 30, 2010 Share Posted January 30, 2010 Hello, I have a form that works good at storing the data to a database, but now I want it to also send the data to a dynamic page. I can get it to send the data to a target page by changing the form action to the target filename, but I can't get it to do both. What do I need to do to accomplish my goal? Thanks <?php $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; $zip = $_POST['zip']; //if( !$rs ) {echo ("<p class='style3'>Sign up for our free newsletter!</p>");} $form = " <form action='$self' method='post'> <p class='style3'>First Name: <br><input type='text' name='first_name' size='10'><br> Last Name: <br><input type='text' name='last_name' size='10'><br> Email: <br><input type='text' name='user_email' size='10'><br> Zip Code: <br><input type='text' name='zip' size='10'><br></p> <input type='submit' value='Submit'> </form>"; if( $first_name and $last_name and $user_email and $zip) { $conn=@mysql_connect( "localhost", "username", "password" ) or die( "Err:Conn" ); #select the specified database $rs = @mysql_select_db( "db name", $conn) or die( "Err:Db" ); #create the query $sql = "insert into newsletter ( first_name, last_name, user_email, zip ) values ( \"$first_name\", \"$last_name\", \"$user_email\", \"$zip\" )"; #execute query $rs = mysql_query( $sql, $conn ); if( $rs ) { echo( "<p class='style5'>Thank you $first_name $last_name. Your address $user_email has been added to our newsletter list. We respect your privacy. Your address will not be shared, sold or given away. Hit the back button to remove this message.</p>" ); } } else { echo $form; } if( !$rs ) {echo ("<p class='style6'>Sign up for our free newsletter!</p>");} ?> Quote Link to comment Share on other sites More sharing options...
TechMistress Posted January 31, 2010 Share Posted January 31, 2010 You can use javascript and the onsubmit function in your submit button to do one of the functions. I'd use it for the targeted page. Something like this: <script type="text/javascript"> function popsubmit(form) { var popName = "formPopUp"; var popStyle = "width=300,height=300,location=yes,resizable=yes"; form.action = "theOthersubmit.php"; form.target = popName; window.open("about:blank",popName,popStyle); } </script> Then your submit button would be something like this: <input type="submit" onclick="popsubmit(this.form);"> Quote Link to comment Share on other sites More sharing options...
okrobie Posted January 31, 2010 Author Share Posted January 31, 2010 WOW!! That's fantastic. Thank you very much TechMistress you're the greatest!! I'll let you know how it works. Quote Link to comment Share on other sites More sharing options...
okrobie Posted January 31, 2010 Author Share Posted January 31, 2010 TechMistress, The popup works beautifully, but it's not sending the data to the database. What am I doing wrong? Thanks. <?php $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; $zip = $_POST['zip']; //if( !$rs ) {echo ("<p class='style3'>Sign up for our free newsletter!</p>");} $form = " <form action='$self' method='post'> <p class='style3'>First Name: <br><input type='text' name='first_name' size='10'><br> Last Name: <br><input type='text' name='last_name' size='10'><br> Email: <br><input type='text' name='user_email' size='10'><br> Zip Code: <br><input type='text' name='zip' size='10'><br></p> <input type='submit' onclick='popsubmit(this.form)' > </form>"; if( $first_name and $last_name and $user_email and $zip) { $conn=@mysql_connect( "localhost", "username", "password" ) or die( "Err:Conn" ); #select the specified database $rs = @mysql_select_db( "db name", $conn) or die( "Err:Db" ); #create the query $sql = "insert into newsletter ( first_name, last_name, user_email, zip ) values ( \"$first_name\", \"$last_name\", \"$user_email\", \"$zip\" )"; #execute query $rs = mysql_query( $sql, $conn ); if( $rs ) { echo( "<p class='style5'>Thank you $first_name $last_name. Your address $user_email has been added to our newsletter list. We respect your privacy. Your address will not be shared, sold or given away. Hit the back button to remove this message.</p>" ); } } else { echo $form; } if( !$rs ) {echo ("<p class='style6'>Sign up for our free newsletter!</p>");} ?> Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 31, 2010 Share Posted January 31, 2010 try... <?php // show everything error_reporting(E_ALL); $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; $zip = $_POST['zip']; //if( !$rs ) {echo ("<p class='style3'>Sign up for our free newsletter!</p>");} $form = "<form action='".$self."' method='post'>"; $form .= "<p class='style3'>First Name: <br><input type='text' name='first_name' size='10'><br>"; $form .= "Last Name: <br><input type='text' name='last_name' size='10'><br>"; $form .= "Email: <br><input type='text' name='user_email' size='10'><br>"; $form .= "Zip Code: <br><input type='text' name='zip' size='10'><br></p>"; $form .= "<input type='submit' onclick='popsubmit(this.form)' >"; $form .= "</form>"; if( $first_name and $last_name and $user_email and $zip) { $conn = mysql_connect( "localhost", "username", "password" ) or die( "Err: Conn<br /><br />" . mysql_error() ); #select the specified database $rs = mysql_select_db( "db name", $conn) or die( "Err: Db<br /><br />" . mysql_error() ); #create the query $sql = "insert into newsletter ( first_name, last_name, user_email, zip ) values ( '".$first_name."', '".$last_name."', '".$user_email."', '".$zip."' )"; #execute query $rs = mysql_query( $sql ); if( $rs ) { echo( "<p class='style5'>Thank you ".$first_name . " " . $last_name.". Your address ".$user_email." has been added to our newsletter list. We respect your privacy. Your address will not be shared, sold or given away. Hit the back button to remove this message.</p>" ); } } else { echo $form; } if( !$rs ) { echo ("<p class='style6'>Sign up for our free newsletter!</p>"); } ?> see how that goes... Quote Link to comment Share on other sites More sharing options...
okrobie Posted January 31, 2010 Author Share Posted January 31, 2010 Hi MasterACE14, Thanks for the help. The result was that it still didn't send the data to the database but I got a bunch of notices. Notice: Undefined index: first_name in /home/thewebpl/public_html/americancitizen/default.php on line 132 Notice: Undefined index: last_name in /home/thewebpl/public_html/americancitizen/default.php on line 133 Notice: Undefined index: user_email in /home/thewebpl/public_html/americancitizen/default.php on line 134 Notice: Undefined index: zip in /home/thewebpl/public_html/americancitizen/default.php on line 135 Notice: Undefined variable: rs in /home/thewebpl/public_html/americancitizen/default.php on line 167 Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 31, 2010 Share Posted January 31, 2010 add after $self... echo $self; just so you can double check it's what you expect it to be. because according to the notice's the form isn't submitting the values. Maybe it is but not to the right file? Quote Link to comment Share on other sites More sharing options...
okrobie Posted January 31, 2010 Author Share Posted January 31, 2010 I tried adding " echo $self; " but it produced no echo. The form does connect with the database and send data when I remove the " onclick='popsubmit(this.form);' " and replace it with " value='submit' " I'm not sure if that answers your concern about the wrong file. The popup does work however. Thanks again, okrobie Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 31, 2010 Share Posted January 31, 2010 change this line... $form = "<form action='".$self."' method='post'>"; to... $form = "<form action='thisfile.php' method='post'>"; replace that with the filename that this form is in... now put that same file name in here if you haven't already... form.action = "theOthersubmit.php"; Quote Link to comment Share on other sites More sharing options...
okrobie Posted January 31, 2010 Author Share Posted January 31, 2010 Thanks for the ideas MasterACE14. I tried substituting the filename for $self and it didn't improve the results. The java part still worked well however. Yes I had the correct filename in the javascript. Where do we go from here? Thanks again, okrobie Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted February 1, 2010 Share Posted February 1, 2010 can you please post your current script now. Quote Link to comment Share on other sites More sharing options...
okrobie Posted February 1, 2010 Author Share Posted February 1, 2010 Thanks MasterACE14, I appreciate your help. <?php // show everything error_reporting(E_ALL); $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; $zip = $_POST['zip']; //if( !$rs ) {echo ("<p class='style3'>Sign up for our free newsletter!</p>");} $form = "<form action='".$self."' method='post'>"; $form .= "<p class='style3'>First Name: <br><input type='text' name='first_name' size='10'><br>"; $form .= "Last Name: <br><input type='text' name='last_name' size='10'><br>"; $form .= "Email: <br><input type='text' name='user_email' size='10'><br>"; $form .= "Zip Code: <br><input type='text' name='zip' size='10'><br></p>"; $form .= "<input type='submit' onclick='popsubmit(this.form)' >"; $form .= "</form>"; //echo ($self); if( $first_name and $last_name and $user_email and $zip) { $conn = mysql_connect( "localhost", "username", "password" ) or die( "Err: Conn<br /><br />" . mysql_error() ); #select the specified database $rs = mysql_select_db( "dbname", $conn) or die( "Err: Db<br /><br />" . mysql_error() ); #create the query $sql = "insert into newsletter ( first_name, last_name, user_email, zip ) values ( '".$first_name."', '".$last_name."', '".$user_email."', '".$zip."' )"; #execute query $rs = mysql_query( $sql ); if( $rs ) { echo( "<p class='style5'>Thank you ".$first_name . " " . $last_name.". Your address ".$user_email." has been added to our newsletter list. We respect your privacy. Your address will not be shared, sold or given away. Hit the back button to remove this message.</p>" ); } } else { echo $form; } if( !$rs ) { echo ("<p class='style6'>Sign up for our free newsletter!</p>"); } ?> Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted February 1, 2010 Share Posted February 1, 2010 where abouts is your javascript? Quote Link to comment Share on other sites More sharing options...
okrobie Posted February 1, 2010 Author Share Posted February 1, 2010 Whoops! Sorry about that. It resides in the header. I'm just going to post the script because it's on a big page with a lot of HTML garbage. Hope you don't mind. <script type="text/javascript"> function popsubmit(form) { var popName = "formPopUp"; var popStyle = "width=300,height=300,location=yes,resizable=yes"; form.action = "newsletter.php"; form.target = popName; window.open("about:blank",popName,popStyle); } </script> Quote Link to comment Share on other sites More sharing options...
okrobie Posted February 1, 2010 Author Share Posted February 1, 2010 MasterACE14, I created a page with everything on it. I hope this helps. As it presently sits it will do the popup but not connect to the database. Thanks for your help, okrobie <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <script type="text/javascript"> function popsubmit(form) { var popName = "formPopUp"; var popStyle = "width=300,height=300,location=yes,resizable=yes"; form.action = "newsletter.php"; form.target = popName; window.open("about:blank",popName,popStyle); } </script> </head> <body> <?php // show everything error_reporting(E_ALL); $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; $zip = $_POST['zip']; //if( !$rs ) {echo ("<p class='style3'>Sign up for our free newsletter!</p>");} $form = "<form action='".$self."' method='post'>"; $form .= "<p class='style3'>First Name: <br><input type='text' name='first_name' size='10'><br>"; $form .= "Last Name: <br><input type='text' name='last_name' size='10'><br>"; $form .= "Email: <br><input type='text' name='user_email' size='10'><br>"; $form .= "Zip Code: <br><input type='text' name='zip' size='10'><br></p>"; $form .= "<input type='submit' onclick='popsubmit(this.form)' >"; $form .= "</form>"; echo ($self); if( $first_name and $last_name and $user_email and $zip) { $conn = mysql_connect( "localhost", "thewebpl_admin", "1460aa" ) or die( "Err: Conn<br /><br />" . mysql_error() ); #select the specified database $rs = mysql_select_db( "thewebpl_amcitizen", $conn) or die( "Err: Db<br /><br />" . mysql_error() ); #create the query $sql = "insert into newsletter ( first_name, last_name, user_email, zip ) values ( '".$first_name."', '".$last_name."', '".$user_email."', '".$zip."' )"; #execute query $rs = mysql_query( $sql ); if( $rs ) { echo( "<p class='style5'>Thank you ".$first_name . " " . $last_name.". Your address ".$user_email." has been added to our newsletter list. We respect your privacy. Your address will not be shared, sold or given away. Hit the back button to remove this message.</p>" ); } } else { echo $form; } if( !$rs ) { echo ("<p class='style6'>Sign up for our free newsletter!</p>"); } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
okrobie Posted February 7, 2010 Author Share Posted February 7, 2010 Hi all, I'm still looking for a solution to this problem. Can someone please help? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.