Jump to content

Recommended Posts

Hi all,

 

Just wondering if someone could assist me with the following issue. I have a web form which simply consists of the following:

<form action="" class="newsletter">
<strong>Enter your email</strong><br />
to receive our latest news and proposals
<div>
<inout type="text" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a>
</div>
</form>

 

What I wanted to do was send the submitted form to an email address and a database.

 

I cobbled the following together for the database:

 

function add_to_db($data){
    $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
    mysql_select_db("dbname", $link) or die ('Can\'t use this DB : ' . mysql_error());
    $values = '';
    $flds = '';
    foreach ($data as $f => $val){
        $values .= (empty($values) ? "'".$val."'" : ",'".$val."'");
        $flds .= (empty($flds) ? $f : ','.$f);
    }

    $sql = "INSERT INTO contact ({$flds}) VALUES({$values})";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
    mysql_close($link);
} 

 

However, I'm not sure of where or how to add the email function for this so it send the submitted form to an email account as well as the database.

 

Any help much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/
Share on other sites

<?php
$GLOBALS['_db_connection'];

function _display_error($error) {
    if (ini_get('display_errors') !== false) {
        echo $error;
    }
}

function db_connect($username, $password, $dbname = null, $host = 'localhost') {
    global $_db_connection;
    $_db_connection = mysql_connect($host, $username, $password) or _display_error(mysql_error());
    if (null !== $dbname) {
        mysql_select_db($dbname) or _display_error(mysql_error());
    }
}

function db_query($query) {
    global $_db_connection;
    return mysql_query($query, $_db_connection);
}

function db_close() {
    global $_db_connection;
    if ($_db_connection) {
        mysql_close($_db_connection);
    }
}

/**
* In here comes all the signup logic
*/
function newsletter_signup($email) {
    $sql = "INSERT INTO contact (email_address) VALUES ('$email')";
    db_query($sql);
    mail(..);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-827843
Share on other sites

Hi,

 

Sorry, I didn't make myself very clear.

 

When someone enters their details into the simple web form that I have I want to send that data to an email address and equally I want to send their submitted email address to a database for storage.

 

So, the PHP needs to firstly send the email address to the database and then send it to a specified email address, this is the part that I'm struggling with.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-827880
Share on other sites

Could you possibly assist me?

 

If you look at the PHP Manual for the Mail() function you will see that they give examples of how to send emails with PHP. I would suggest reading up on that and attempting to implement one of the examples that best suit your needs. There are also plenty of Google Resources available for how to send mail with PHP. If you cannot attempt to try it and if you fail provide the code for us to assist you, then perhaps posting this in the freelance section is the way you want to go and offer someone money to code it for you.

 

We are here to help, not write the code for you. At least give it a try as you should have all the resources you need from the manual and or by searching google.

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-827891
Share on other sites

Hi,

 

Yes, I agree...I have come up with the following and would appreciate it if you could cast your eyes over it and let me know if I'm correct:

<?php

function add_to_db($data){
$link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
mysql_select_db("dbname", $link) or die ('Can\'t use this DB : ' . mysql_error());
$values = '';
$flds = '';
foreach ($data as $f => $val){
	$values .= (empty($values) ? "'".$val."'" : ",'".$val."'");
	$flds .= (empty($flds) ? $f : ','.$f);
}

$sql = "INSERT INTO dbname ({$flds}) VALUES({$values})";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
mysql_close($link); 
}

empty($_POST['email']) ? $email='' : $email = $_POST['email'];

	$verif_box = $_REQUEST["verif_box"];
	if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
		require_once 'includes/class.ext.mail.php';

		$options = array(
			'To' => 'name@someemail.co.uk',			    			/* Sets the To email address for the message */
			'ToName' => '', 										/* Sets the From name of the message */
			'From' => '', 											/* Sets the From email address for the message */
			'FromName' => '', 										/* Sets the From name of the message */
			'Subject' => 'Contact form',							/* Sets the Subject of the message */
			'Mailer' => 'mail',										/* Method to send mail: ("mail", "sendmail", or "smtp") */
			'Host'=> '',											/*  Sets the SMTP hosts. All hosts must be separated by a semicolon.
		You can also specify a different port for each host by using this
		format: [hostname:port] (e.g. "smtp1.example.com:25;smtp2.example.com").
		Hosts will be tried in order. */
			'Port' => 25,											/* Sets the default SMTP server port */
			'SMTPAuth' => false,									/* Sets SMTP authentication. Utilizes the Username and Password variables. */
			'Username' =>'',										/* Sets SMTP username. */
			'Password' => '',										/* Sets SMTP password. */
		);
		$fields = array(
				'E.mail (E-mail address)' => 'email',
				'Messaggio (Message)' => 'message'
				);

				$mail = new ExtMail($options);
				if(empty($options['From']))	$mail->AddFromAddress($email,$name);
				$mail->AddAddress($options['To'],$options['ToName']);

}
?>

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-827984
Share on other sites

Ok, let's start out by "How" the form is not working. It looks like you pulled in a 3rd party app to utilize this. That is fine, however, I do not know how this form gets submitted, so if you setup your form properly. Might I ask, why did you go for a 3rd party app to handle this instead of just using the mail function?

 

 

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-828484
Share on other sites

Hi,

 

I'm simply trying to cobble things together as I have never done this before and need to get the form working ASAP.

 

I'm not even sure as to how to process the following webform:

 

<form action="" class="newsletter">
<strong>Enter your email</strong><br />
to receive our latest news and proposals
<div>
<inout type="text" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a>
</div>
</form>

 

I have changed some of the values as per the following:

 

<form action=signup.php"" class="newsletter">

<strong>Enter your email</strong><br />

to receive our latest news and proposals

<div>

<inout type="submit" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a>

</div>

</form>

 

However, as you know the PHP isn't working and I'm not then that the form will work.

 

Would really appreciate some help and how best to proceed.

Link to comment
https://forums.phpfreaks.com/topic/157125-newsletter-form/#findComment-828522
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.