Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » Having trouble with "submit" button php function RSS

Having trouble with "submit" button php function

This thread was started by TWyPGn and has been viewed 860 times, and contains 15 replies, with the last reply made by Wired.
Post Reply
1
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 15 '09 at 09:42 AM
      Posts: 21
Hello, I am making a contact form for my website with a submit button where a form data is sent to you through email, and it also generates a message "Thank you for your interest. We'll respond to you shortly" after the "submit" button is clicked.

For the php script for submit button, I took help from Elizabeth Castro's book "HTML, XHTML & CSS", but it is not generating the desired output, instead the script page opens on clicking submit. Let me mention here that I don't know anything about php/javascript. Following are the html codes for the contact form, and php code for submit button:

HTML:


<div id="form">

                <form action="emailform.php" method="post">
                
                    <p class="legend">Personal Information</p>
                    
                    <fieldset id="personal">
                        <label>Name:</label><input type="text" name="name" size="30" maxlength="40" /> <br />
                        <label>City &amp; Country:</label><input type="text" name="city &amp; country" size="30" /> <br />

                        <label>Phone:</label><input type="text" name="phone" size="30" /> <br />
                        <label>Email Address:</label><input type="text" name="email address" size="30" maxlength="40" /><br /> 
                        
                    </fieldset>


                    <p class="legend">Subject</p>
                    
                    <fieldset id="choices">

                        <p id="subject"><label>Tell us what your query is about:</label>
                        <select name="subject" >
                            <option value="Product Inquiry">Product Inquiry</option>
                            <option value="Order Inquiry">Order Inquiry</option>
                            <option value="Shipping Inquiry">Delivery Inquiry</option>
                            <option value="General Inquiry">General Inquiry</option>
                        </select>
                        </p>

                    </fieldset>

                    <p class="legend">Your question or message here</p>
                    
                    <fieldset id="questions">

                        <textarea name="comments" rows="3" cols="40">Yor questions or message here</textarea>
                    </fieldset>
                    
                    <p id="buttons">
                        <input type="submit" name="submit" value="Submit"  />
                    </p>
                    
                </form>
            </div>

[b]PHP from Elizabeth Castro's website:[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Emailing Form Data</title>
<style type="text/css">
code {color:#F00C4D;font-weight:bold;font-size:1.2em}
i {color: #6D0CF0}
th, td {padding:.1em;border:1px solid blue;text-align:left}
</style>
</head>
<body>


<?php
//This is a very simple PHP script that outputs the name of each bit of information (that 
//corresponds to the <code>name</code> attribute for that field) along with the value that was
//sent with it right in the browser window, and then sends it all to an email address (once you've
//added it to the script).

if (empty($_POST)) {
    print 
"<p>No data was submitted.</p>";
    print 
"</body></html>";
    exit();
}

//Creates function that removes magic escaping, if it's been applied, from values and then 
removes extra newlines and returns to foil spammersThanks Larry Ullman!
function 
clear_user_input($value) {
    if (
get_magic_quotes_gpc()) $value=stripslashes($value);
    
$valuestr_replace"\n"''trim($value));
    
$valuestr_replace"\r"''$value);
    return 
$value;
    }


if (
$_POST['comments'] == 'your questions or comments here'$_POST['comments'] = '';    

//Create body of message by cleaning each field and then appending each name and value to it

$body ="Here is the data that was submitted:\n";

foreach (
$_POST as $key => $value) {
    
$key clear_user_input($key);
    
$value clear_user_input($value);
    if (
$key=='extras') {
        
    if (
is_array($_POST['extras']) ){
        
$body .= "$key: ";
        
$counter =1;
        foreach (
$_POST['extras'] as $value) {
                
//Add comma and space until last element
                
if (sizeof($_POST['extras']) == $counter) {
                    
$body .= "$value\n";
                    break;}
                else {
                    
$body .= "$value, ";
                    
$counter += 1;
                    }
                }
        } else {
        
$body .= "$key: $value\n";
        }
    } else {

    
$body .= "$key: $value\n";
    }
}

extract($_POST);
//removes newlines and returns from $email and $name so they can't smuggle extra email addresses for spammers
$email clear_user_input($email);
$name clear_user_input($name);

//Create header that puts email in From box along with name in parentheses and sends bcc to alternate address
$from='From: '$email "(" $name ")" "\r\n" 'Bcc: [email="yourmail@yourdomain.com"]yourmail@yourdomain.com[/email]' "\r\n";


//Creates intelligible subject line that also shows me where it came from
$subject 'Query from Web Site';

//Sends mail to me, with elements created above
mail ('youremail@yourdomain.com'$subject$body$from);


?>

<p>Thanks for your interest! We'll respond to you shortly.</p>


</body>
</html>
-----------------------------------
If anybody could please help me with this php code and submit button to work properly, I'll be very grateful. Thanks!
Last edited June 15 '09 at 05:38 PM by Wired ("added BBCODE"). Reply

Advertisement Register for free to hide these ads and participate in discussions!

2
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 15 '09 at 09:48 AM
      Posts: 790
turning off magic quotes is problematic at best. host can keep this from working. the right way to go about this is to use $value = urlencode($value); and just ditch the stripslashes and magic workaround. you will need urldecode on the other end.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

3
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 15 '09 at 09:05 PM
      Posts: 21
Thanks Dorky for replying with a solution to fix the problem. Here let me say that I don't know ANYTHING about php. So I don't know what the submit php code is and how to edit it. So I'll appreciate if you could tell me how to edit the php code through exact script. Thanks!

4
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 15 '09 at 09:22 PM
      Posts: 790
ok i im not the guy to teach php, im still learning. so instead ill share a small yet functional mail script that doesnt require so much changing and overhead.


//this part goes in an html file you create. mine is msg.html
<head>
</head>
<body>
<html>
<form method='post' action='sendmail.php'>
Contact Info:<input class='textarea' name='email' type='text' /><br/><br/>
Drop us a Line:<br/>
<textarea class='textarea' name='message' rows='25' cols='50' >
</textarea><br/>
<button name='submit' type='submit' class='submit'>Send Mail</button>
</form>
</body>
</html>

//then this goes in a php file. i call mine sendmail.php
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

mail( "you@yoursite.com", "Site Mail",
$message, "From: $email" );
echo "Thank You";
?>

small but it does just what i want.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

5
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 16 '09 at 04:42 PM
      Posts: 21
Thanks a lot Dorky for the php code. It works. Do you any simple online tutorials on PHP that teach more complex php submit code? I have come across online tutorials but I don't understand anything. Or if you could suggest any books on php, I'll appreciate that. Anyway, thanks once again.

6
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 16 '09 at 05:22 PM
      Posts: 790
www.w3schools.com
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

7
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 16 '09 at 07:16 PM
      Posts: 21
Thanks Dorky! Hope I'm able to get the help from "w3schools" tutorials. Thanks!

8
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 17 '09 at 01:01 PM
      Posts: 790
no prob. hope it helps. when w3schools doesnt elaborate enough to get the point made come here and we will solve it.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

9
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 17 '09 at 05:12 PM
      Posts: 21
Hello, still no luck in getting my "submit" button work. I wanted to know if submit button php code works when the website is launched, or can one check its working in design phase. I have tried various php codes that send data to an email but nothing has worked. I thought since I haven't launched the website it might not be working properly. Please help!!!

10
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 17 '09 at 05:21 PM
      Posts: 790
would need your server info before making that call as far as how you are testing at this point.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

11
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 17 '09 at 05:24 PM
      Posts: 21
I haven't launched the website yet. I think launching the website and then running the php code might suggest where the problem is. What do you think? Anyway, thanks for replying.

12
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 17 '09 at 05:25 PM
      Posts: 790
yeah if you dont have it on a server with php installed it wont work.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

13
345 points at 100% Donor Moderator Repute WDFplus Member
Wired, Admin and WDF Alien Overlord Home page   Private message  
Posted June 17 '09 at 05:28 PM
      Posts: 6,275
How to Send Email from a PHP Script

Configure PHP to Use a Local Mail Server for Sending Mail
How to Configure PHP to Use a Remote SMTP Server for Sending Mail

In short, you have to check your php.ini under the [mail function] to make sure it's set up correctly. Otherwise, you can't use the mail() function.
Admin at houseofhelp.com
WDF Resources: The Rules
Founder/Creator/Admin of ZE SECRET PROJECT!

Was another WDF member's post helpful? Click the positive rating button () above the post.

14
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 17 '09 at 05:33 PM
      Posts: 21
Thanks! By the way how do I know if the server or my web host has php installed?

15
View TWyPGn's reputation
TWyPGn, WDF User Private message  
Posted June 17 '09 at 05:39 PM
      Posts: 21
Thanks Wired for sending the links of articles on this form-email stuff. I hope all this helps. I am total novice in php, so really don't have any idea what to do or what is required. Anyway, thanks!

16
345 points at 100% Donor Moderator Repute WDFplus Member
Wired, Admin and WDF Alien Overlord Home page   Private message  
Posted June 17 '09 at 06:12 PM
      Posts: 6,275
Ask your host what you are paying for.
Admin at houseofhelp.com
WDF Resources: The Rules
Founder/Creator/Admin of ZE SECRET PROJECT!

Was another WDF member's post helpful? Click the positive rating button () above the post.

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 18 March 10 '10 08:52 AM
php radio button form ketanco PHP 4 September 28 '08 11:51 AM
JS menu alignment in IE robbieM Javascript, AJAX, and JSON 0 January 28 '05 09:33 AM
Will pay for custom php function jf1288 Request a Service 0 December 19 '04 01:20 AM
Timer krazy Javascript, AJAX, and JSON 172 July 16 '03 01:38 PM