Need help for a simple php email script or opt in!

13 replies
Hi guys, I spent quite a bit time trying to get an opt in form worked on my new ecommerce website. I know nothing about php..:confused:

This is the form:

<form name="form" action="send_form_email.php" method="post">
<input name="subject" type="text" id="subject" value="Join Our Email List" " onfocus="if(!this._haschanged){this.value=''};this ._haschanged=true;" />
<input type="submit" name="submit" value="Submit" class="button" /> </form>


This is the php i used
<?php


// Contact subject
$subject ="$subject";

// Details
$message="$detail";


// Mail of sender
$mail_from="$customer_mail";

// From
$header="from: $name <$mail_from>";


// Enter your email address
$to ='myemail@mysitemail.com';

$send_form_email=mail($to,$subject,$message,$heade r);


// Check, if message sent to your email
// display message "We've recived your information"
if($send_form_email){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>


I have tried so many times , it didn't work for me.
I only need an email field and a submit button. still i didn't make it work.
Someone please help me.

I don't intend to have many subs. I only want something that does the work.
#email #opt #php #script #simple
  • {{ DiscussionBoard.errors[7615049].message }}
  • Profile picture of the author David V
    Try the green-beast GBCF-v3 by Mike Cherim.
    Hasn't been updated in awhile, but for a simple html site it's more then enough and works very well, pretty easy to setup and customize as well.
    {{ DiscussionBoard.errors[7615265].message }}
  • Profile picture of the author FirstSocialApps
    Lots of errors in this:

    First off you have to pull the data from the form in your PHP

    $subject=$_POST['subject'];

    Second off, you have this:

    $subject ="$subject";
    Which will just change the $subject variable to what is in the quotes so the subject of your email will be "$subject"


    Third off you dont even have a field in the form for the message, however you are setting it in the PHP with $message="$detail"; .. your making the message be "$detail" again it will be exactly what is in the quotes.

    add this to the form:
    <textarea name="message" rows="35" cols="5"></textarea>
    add this to the PHP
    $message=$_POST['message'];

    Third off you have a space in the $header variable in the mail() function. Its showing as $head r

    Fourth off your onfocus code is not very good. If you want to clear the value of the field when the user clicks it you can use onclick="$(this).val('')" if you have JQUERY or you can always use the placeholder="text here" attribute (html 5)

    You also should clean your data once you get it
    {{ DiscussionBoard.errors[7615791].message }}
    • Profile picture of the author webily
      I'm a PHP developer for about 10 years and I always make the forms the same way. Below I show simple full working example.
      It manages errors (missing fields) and keeps the already filled-in fields after submitting. This way the visitor will not need to enter all the fields again in case of error.
      Note that I keep this example simple, suitable for beginners. It can improved a lot using by Javascript, Ajax, data format validation using RegExp, etc.

      I have attached the file to keep the proper formatting.
      I don't find it easy to format the code here in the post.
      Signature
      Professional WordPress Plugin Developer
      {{ DiscussionBoard.errors[7616971].message }}
    • Profile picture of the author webily
      $subject = "$subject";

      This actually dosen't change anything.
      The subject will remain the same. PHP parses variables when they are enclosed in double quotes (").

      You can test this:

      $subject = 'Test';
      $subject = "$subject";
      echo $subject; //the output will be: Test

      $subject = '$subject';
      echo $subject; //the output will be: $subject
      Signature
      Professional WordPress Plugin Developer
      {{ DiscussionBoard.errors[7616997].message }}
  • Profile picture of the author Giselle85G
    Thanks so much for your Patience for pointing out all the errors.
    as you can see I don't know much about it at all.
    I only need one text area to collect email addresses and one submit button.
    I don't intend to have a message field.
    My problem is when i test it, i received the email but without the email address it should have from the only text area.
    {{ DiscussionBoard.errors[7618616].message }}
  • Profile picture of the author FirstSocialApps
    Ok I see, Thought you were trying to get a message. Ok well here it is quick and simple then:

    Change this line:

    $subject ="$subject";

    to this:
    $subject="New opt in"

    Change this line:
    $message="$detail";
    to
    $message=$_POST['subject'];

    The net result will be that you get the info they enter in that text field to be the body of the email.

    **Note this will work, and I gave it to you this way because it only requres 2 changes. However this is a really crappy to do this over all. There is no validation, and the headers in the email are all janked up by the code you have. You really should just hire someone on Elance to do this for you, its like a $20 job.
    {{ DiscussionBoard.errors[7618690].message }}
  • Profile picture of the author festi9
    Originally Posted by Giselle85G View Post

    Hi guys, I spent quite a bit time trying to get an opt in form worked on my new ecommerce website. I know nothing about php..:confused:

    This is the form:

    <form name="form" action="send_form_email.php" method="post">
    <input name="subject" type="text" id="subject" value="Join Our Email List" " onfocus="if(!this._haschanged){this.value=''};this ._haschanged=true;" />
    <input type="submit" name="submit" value="Submit" class="button" /> </form>


    This is the php i used
    <?php


    // Contact subject
    ="";

    // Details
    ="";


    // Mail of sender
    ="";

    // From
    ="from: <>";


    // Enter your email address
    ='myemail@mysitemail.com';

    =mail(,,,);


    // Check, if message sent to your email
    // display message "We've recived your information"
    if(){
    echo "We've recived your contact information";
    }
    else {
    echo "ERROR";
    }
    ?>


    I have tried so many times , it didn't work for me.
    I only need an email field and a submit button. still i didn't make it work.
    Someone please help me.

    I don't intend to have many subs. I only want something that does the work.
    the html should be

    <form name="form" action="send_form_email.php" method="post">
    Email:
    <input name = "email" type=""text>
    <input name="subject" type="text" id="subject" value="Join Our Email List" " onfocus="if(!this._haschanged){this.value=''};this ._haschanged=true;" />
    <input type="submit" name="submit" value="Submit" class="button" /> </form>

    The PHP file should be
    <?php
    $subject = $_post['subject'];
    $details = 'your message/ mail body here';


    // Contact subject
    $subject ="$subject";

    // Details
    $message="$detail";


    // Mail of sender
    // this is the id that is used to send email
    $mail_from="your mail id here";

    // From
    $header="from: <$mail_from>";


    // Enter your email address
    $to =$_POST['email'];

    $send_form_email=mail($to,$subject,$message,$heade r);


    // Check, if message sent to your email
    // display message "We've recived your information"
    if($send_form_email){
    echo "We've recived your contact information";
    }
    else {
    echo "ERROR";
    }
    ?>

    so this is the complete code if you still get error then contact me i will get that fixed.
    {{ DiscussionBoard.errors[7620637].message }}
  • Profile picture of the author festi9
    This will still not work as there are errors in the complete script.
    please let me know
    i would suggest you to go to fiverr for getting that fixed
    utsavat's profile page on Fiverr.com
    i think the above link would help
    {{ DiscussionBoard.errors[7620651].message }}
    • Profile picture of the author Giselle85G
      Hi guys,
      thanks so much for helping me out.
      I made it work.
      Because selecting email addresses is not the part I focused on at the moment, more just for the looking and if I do receive some subs. that will be a bonus.

      I don't know why i can't quote from this thread.
      {{ DiscussionBoard.errors[7628059].message }}

Trending Topics