Tutorials for passing value data to email with PHP

2019-08-31 03:04发布

问题:

I have built a html form in which a user has to specify which product he likes to order and depending on his answer other input fields arise. I have used some javascript for that. But now i want to data the user enters to be passed on to email via php. I know very little of php and scripting in general (let's say about nothing) but i did find some tutorials and templates on it. The only thing is that they all seem slightly different and specific to a form (mostly contact forms). And if i follow them closely it works fine, it's just that when i want to customize a little i seem to get terribly lost.

I have posted this before (Can i post data from a select drop down to email using PHP?) and have gotten some answers. It's just that i didn't formulate my initial problem well enough and edited it afterwards, but i don't know if that's the way to go on this forum. So i posted a new question (sorry if that's overkill, i'm pretty new to all of this). This is my html:

            <form method="post" name="contact_form" action="contact.php">                       
                <div class="form-div1">
                    <label for="profile" class="label1">Select your profile</label>
                    <br>
                    <select id="user-type" class="selectlist">
                        <option value="option1">I'm a first-time user</option>
                        <option value="option2">I would like to renew or upgrade</option>
                    </select>
                </div>

                <div class="form-div2">
                    <label for="SEN" class="label2">SEN</label>
                    <br>
                    <input type "text" name="input1" class="input1">
                </div>

                <div class="form-div3">
                    <label for="email" class="label3">Email Address</label>
                    <br>
                    <input type "text" name="input2" class="input2">
                </div>

                <div class="form-div4">
                    <label for="product_choice" class="label4" name="select_menu">Select your product</label>
                    <br>
                    <select id="product" class="selectlist">
                        <option value="option1">JIRA</option>
                        <option value="option2">Confluence</option>
                        <option value="option3">JIRA Service Desk</option>
                        <option value="option4">Stash</option>

                        <option value="option5">Other</option>
                    </select>
                </div>

                <div class="form-div42">
                    <label for="product" class="label42">Specify your product</label>
                    <br>
                    <input type "text" name="input2" class="input2">
                </div>

                <div class="form-div5">
                    <label for="license_choice" class="label5">Select your license</label>
                    <br>
                     <select id="select" class="selectlist">
                        <option value="option1">25 users</option>
                        <option value="option2">50 users</option>
                        <option value="option3">100 users</option>
                    </select>
                </div>

                <div class="input_box_atlassian">
                    <input type="submit" name="submit" value="Submit" class="submit-button-atl" />
                </div>
            </form>

Now i want to build working php so i can pass the data to an email. Can somebody point me in the right direction or preferably show me some tutorials that tackle this issue more closely.

回答1:

I havent dont any validation, try client side & server side validation along with code Change the HTML as below

 <form method="post" name="contact_form" action="contact.php">                       
                <div class="form-div1">
                    <label for="profile" class="label1">Select your profile</label>
                    <br>
                    <select id="user_type" name="user_type" class="selectlist">
                        <option value="I'm a first-time user">I'm a first-time user</option>
                        <option value="I would like to renew or upgrade">I would like to renew or upgrade</option>
                    </select>
                </div>

                <div class="form-div2">
                    <label for="SEN" class="label2">SEN</label>
                    <br>
                    <input type "text" name="SEN" class="SEN">
                </div>

                <div class="form-div3">
                    <label for="email" class="label3">Email Address</label>
                    <br>
                    <input type "text" name="email" class="email">
                </div>

                <div class="form-div4">
                    <label for="product_choice" class="label4" name="select_menu">Select your product</label>
                    <br>
                    <select id="product" name="product" class="selectlist">
                        <option value="JIRA">JIRA</option>
                        <option value="Confluence">Confluence</option>
                        <option value="JIRA Service Desk">JIRA Service Desk</option>
                        <option value="Stash">Stash</option>
                        <option value="Other">Other</option>
                    </select>
                </div>

                <div class="form-div42">
                    <label for="product" class="label42">Specify your product</label>
                    <br>
                    <input type "text" name="product_specify" id="product_specify" class="input2">
                </div>

                <div class="form-div5">
                    <label for="license_choice" class="label5">Select your license</label>
                    <br>
                     <select id="license_choice" name="license_choice" class="selectlist">
                        <option value="25">25 users</option>
                        <option value="50">50 users</option>
                        <option value="100">100 users</option>
                    </select>
                </div>

                <div class="input_box_atlassian">
                    <input type="submit" name="submit" value="Submit" class="submit-button-atl" />
                </div>
            </form>

And make another PHP file by the name contact.php & paste the below code.

<?php
if($_REQUEST['submit']=='Submit'){
    $user_type=$_REQUEST['user_type'];
    $sen=$_REQUEST['SEN'];
    $email=$_REQUEST['email'];
    $product=$_REQUEST['product'];
    $product_specify=$_REQUEST['product_specify'];
    $license_choice=$_REQUEST['license_choice'];

    $to      = 'nobody@example.com'; //to whom the email have to besnd
    $message = "User Type: $user_type \n Sen: $sen \n Email: $email \n Product: $product \n Product Specified: $product_specify \n License: $license_choice"; //content of the email
    $subject = 'hello';  //shown as subject of the email
    $headers = 'From: webmaster@example.com' . "\r\n" . //eamil header information
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    if(mail($to, $subject, $message, $headers)){
    echo "Mail Sucessfully Sent";
    } else {
    echo "Something went wrong";
    }
}
?>

This will get the content from the form HTML & email its contents, however only if the email server is configured correctly



回答2:

Consider the following URL http://www.freecontactform.com/email_form.php

Cheers!!



标签: php forms