Jump to content

One form, 2 functions?


okrobie

Recommended Posts

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>");}

?>

Link to comment
Share on other sites

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);"> 

 

 

Link to comment
Share on other sites

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>");}

?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>"); }

?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.