[Help] PHP Form - Not Working

Discussion in 'PHP' started by pixelgeek, Jun 1, 2010.

  1. #1
    Hello I am trying to make custom form and I can not get it to work correctly any help will be greatly appreciated here is the coding. the form is at: http://pixelgeek.net/temp2/quote.html

    PHP
    ------------------------------------------------------
    <?php
    // Website Contact Form Generator
    // http://www.tele-pro.co.uk/scripts/contact_form/
    // This script is free to use as long as you
    // retain the credit link

    // get posted data into local variables
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
    $EmailTo = "shawn@pixelgeek.net";
    $Subject = Trim(stripslashes($_POST['Subject']));
    $Name = Trim(stripslashes($_POST['Name']));
    $Tel = Trim(stripslashes($_POST['Tel']));
    $Town = Trim(stripslashes($_POST['Town']));
    $State = Trim(stripslashes($_POST['State']));
    $Zipcode = Trim(stripslashes($_POST['Zipcode']));
    $Vin_Number = Trim(stripslashes($_POST['Vin_Number']));
    $Year = Trim(stripslashes($_POST['Year']));
    $Make = Trim(stripslashes($_POST['Make']));
    $Model = Trim(stripslashes($_POST['Model']));
    $Body = Trim(stripslashes($_POST['Body']));
    $Mileage = Trim(stripslashes($_POST['Mileage']));
    $Interior_Color = Trim(stripslashes($_POST['Interior_Color']));
    $Exterior_Color = Trim(stripslashes($_POST['Exterior_Color']));
    $Doors = Trim(stripslashes($_POST['Doors']));
    $Gears = Trim(stripslashes($_POST['Gears']));
    $Condition = Trim(stripslashes($_POST['Condition']));
    $Cylinders = Trim(stripslashes($_POST['Cylinders']));
    $PWR_Windows = Trim(stripslashes($_POST['PWR_Windows']));
    $Leather = Trim(stripslashes($_POST['Leather']));
    $Diesel = Trim(stripslashes($_POST['Diesel']));
    $PWR_Doors = Trim(stripslashes($_POST['PWR_Doors']));
    $Quad_Cab = Trim(stripslashes($_POST['Quad_Cab']));
    $4x4 = Trim(stripslashes($_POST['4x4']));
    $Sunroof = Trim(stripslashes($_POST['Sunroof']));
    $T-Tops = Trim(stripslashes($_POST['T-Tops']));
    $Dully = Trim(stripslashes($_POST['Dully']));
    $Air-Conditioning = Trim(stripslashes($_POST['Air-Conditioning']));
    $Extended_Cab = Trim(stripslashes($_POST['Extended_Cab']));
    $Turbo = Trim(stripslashes($_POST['Turbo']));
    $Owe_Money = Trim(stripslashes($_POST['Owe_Money']));
    $Amount_Owed = Trim(stripslashes($_POST['Amount_Owed']));
    $Asking_Price = Trim(stripslashes($_POST['Asking_Price']));
    $Comment = Trim(stripslashes($_POST['Comment']));

    // validation
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (!$validationOK) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";
    $Body .= "Town: ";
    $Body .= $Town;
    $Body .= "\n";
    $Body .= "State: ";
    $Body .= $State;
    $Body .= "\n";
    $Body .= "Zipcode: ";
    $Body .= $Zipcode;
    $Body .= "\n";
    $Body .= "Vin_Number: ";
    $Body .= $Vin_Number;
    $Body .= "\n";
    $Body .= "Year: ";
    $Body .= $Year;
    $Body .= "\n";
    $Body .= "Make: ";
    $Body .= $Make;
    $Body .= "\n";
    $Body .= "Model: ";
    $Body .= $Model;
    $Body .= "\n";
    $Body .= "Body: ";
    $Body .= $Body;
    $Body .= "\n";
    $Body .= "Mileage: ";
    $Body .= $Mileage;
    $Body .= "\n";
    $Body .= "Interior_Color: ";
    $Body .= $Interior_Color;
    $Body .= "\n";
    $Body .= "Exterior_Color: ";
    $Body .= $Exterior_Color;
    $Body .= "\n";
    $Body .= "Doors: ";
    $Body .= $Doors;
    $Body .= "\n";
    $Body .= "Gears: ";
    $Body .= $Gears;
    $Body .= "\n";
    $Body .= "Condition: ";
    $Body .= $Condition;
    $Body .= "\n";
    $Body .= "Cylinders: ";
    $Body .= $Cylinders;
    $Body .= "\n";
    $Body .= "PWR_Windows: ";
    $Body .= $PWR_Windows;
    $Body .= "\n";
    $Body .= "Leather: ";
    $Body .= $Leather;
    $Body .= "\n";
    $Body .= "Diesel: ";
    $Body .= $Diesel;
    $Body .= "\n";
    $Body .= "PWR_Doors: ";
    $Body .= $PWR_Doors;
    $Body .= "\n";
    $Body .= "Quad_Cab: ";
    $Body .= $Quad_Cab;
    $Body .= "\n";
    $Body .= "4x4: ";
    $Body .= $4x4;
    $Body .= "\n";
    $Body .= "Sunroof: ";
    $Body .= $Sunroof;
    $Body .= "\n";
    $Body .= "T-Tops: ";
    $Body .= $T-Tops;
    $Body .= "\n";
    $Body .= "Dully: ";
    $Body .= $Dully;
    $Body .= "\n";
    $Body .= "Air-Conditioning: ";
    $Body .= $Air-Conditioning;
    $Body .= "\n";
    $Body .= "Extended_Cab: ";
    $Body .= $Extended_Cab;
    $Body .= "\n";
    $Body .= "Turbo: ";
    $Body .= $Turbo;
    $Body .= "\n";
    $Body .= "Owe_Money: ";
    $Body .= $Owe_Money;
    $Body .= "\n";
    $Body .= "Amount_Owed: ";
    $Body .= $Amount_Owed;
    $Body .= "\n";
    $Body .= "Asking_Price: ";
    $Body .= $Asking_Price;
    $Body .= "\n";
    $Body .= "Comment: ";
    $Body .= $Comment;
    $Body .= "\n";

    // send email
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page
    if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
    }
    else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>
     
    pixelgeek, Jun 1, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    
    $4x4 = Trim(stripslashes($_POST['4x4']));  //<-- naming convention
    //..
    $Body .= $4x4;
    
    PHP:
     
    gapz101, Jun 1, 2010 IP
  3. pixelgeek

    pixelgeek Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I do not understand what I need to change?
    Thanks for your reply but could you go into a little more detail.
     
    pixelgeek, Jun 1, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    php variable starts with $ right? but not number after so. $4x4 is not valid, rename it something like $_4x4 or any...
     
    Last edited: Jun 1, 2010
    gapz101, Jun 1, 2010 IP
  5. pixelgeek

    pixelgeek Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I changed $4x4 to $Drive_4x4 and all other references but still no luck. When I test form at http://pixelgeek.net/temp2/quote.html it takes me to a blank page http://pixelgeek.net/temp2/quote.php I do not understand I am using a similar script for the contact page at http://pixelgeek.net/temp2/contact.html and it works just fine.

    Do you think you could test it so you can see what I mean
    Thanks for your continued help
     
    pixelgeek, Jun 1, 2010 IP
  6. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #6
    no idea, can you show error_log?
     
    gapz101, Jun 1, 2010 IP
  7. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I went overkill for what you wanted, but I figure this would help others. It has not been tested and there are some better ways of achieving faster processing and such, but I wasn't too concerned about it.

    Hope this helps!

    <?php
    // ----------------------------------------------------------------
    // A Better "Website Contact Form"
    // Written by: Trikun3
    // URI: http://trikun3.com/
    // ----------------------------------------------------------------
    
    // Some application defaults.
    $post_array = array();
    $Body = '';
    
    // Configuration Variables.
    $EmailTo = 'YOUR_EMAIL@HERE.COM';
    $Subject = 'YOUR_SUBJECT_HERE';
    
    // Set all of the post data you want to retrieve.
    $retrieve = 'EmailFrom,Subject,Name,Tel,Town,State,Zipcode,Vin_Number,Year,Make,Model,Body,Mileage,Interior_Color,Exterior_Color,Doors,Gears,Condition,Cylinders,PWR_Windows,Leather,Diesel,PWR_Doors,Quad_Cab,4x4,Sunroof,T-Tops,Dully,Air-Conditioning,Extended_Cab,Turbo,Owe_Money,Amount_Owed,Asking_Price,Comment';
    
    // ----------------------------------------------------------------
    
    // Functions.
    function sanitize($input)
    {
    	$safe = filter_input(INPUT_POST, $input, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
    	return $safe;
    }
    
    // ----------------------------------------------------------------
    
    // Set the explode(array) into $items.
    $items = explode(',', $retrieve);
    
    // Foreach post data element, assign it into the post_array with the sanitized value.
    foreach($items as $item)
    {
    	$post_array[$item] = sanitize($item);
    }
    
    // Check that EmailFrom exists.
    if(isset($EmailFrom) AND !empty($EmailFrom))
    {
    	header('Location: error.htm');
    	exit();
    }
    
    // Generate the body with the post_array.
    foreach($post_array as $name => $value)
    {
    	$Body .= "{$name}: $value\n";
    }
    
    // Success or Failure redirection.
    // If the email was sent:
    if(mail($EmailTo, $Subject, $Body, "From: <{$EmailFrom}>"))
    {
    	header('Location: ok.htm');
    	exit();
    }
    else
    {
    	header('Location: error.htm');
    	exit();
    }
    
    /* END OF FILE */
    PHP:
     
    Trikun3, Jun 1, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    @Trikun3

    You don't need:

    // Foreach post data element, assign it into the post_array with the sanitized value.
    foreach($items as $item)
    {
        $post_array[$item] = sanitize($item);
    }
    
    PHP:
    As this can simply be achieved with:

    $post_array = array_map('sanitize', $items);
    PHP:
    But overall your codes neat :)
     
    danx10, Jun 1, 2010 IP
  9. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Foreach is faster than array_map :p
     
    Trikun3, Jun 1, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    Surprising, but the point whether to use foreach or array_map in this case is arguable, faster code vs minimal code.
     
    danx10, Jun 1, 2010 IP
  11. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #11
    Bit off topic but have to ask
    Isn´t faster always better than minimal?

    Personally I rather have a fast website than one that´s "Small" in code size..
     
    n3r0x, Jun 1, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    I also love to have the faster one!
     
    roopajyothi, Jun 1, 2010 IP