1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Contact form help - PHP help

Discussion in 'HTML & Website Design' started by Web_Dev_Chris, Dec 26, 2018.

  1. #1
    Hello,

    Array() is being printed above my contact form and cannot work out why.
    SEMrush
    Here is my code

    HTML
     <div class="footer-section-right">
                <?php include('form_process.php'); ?>
                <form method="post" action="<?= $_SERVER['PHP_SELF']; ?>">
                    <input class="input1" type="text" placeholder="Name" name="name">
                    <input class="input1" type="text" placeholder="Email" name="email">
                    <input class="input1" type ="text" placeholder="Subject" name="subject">
                    <input  class="message" type="text" placeholder="Message" name="message">
    
                    <input class="submit"  type="submit" value="Send">
    
                </form>
            </div>
    
            </div>
        </footer>
    HTML:

    PHP:
    <?php
    
    print_r($_POST);
    
    // define variables and set to empty values
    $name_error = $email_error = /*$phone_error = $url_error = */"";
    $name = $email = /*$phone*/$subject = $message/* = $url */= "";
    
    //form is submitted with POST method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name"]);
            //Check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $name_error = "Only letters and white space allowed";
            }
        }
    
    if (empty($POST["email"])) {
        $email_error = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        //check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email_error = "Invalid email format";
        }
    }
    /* 
    if (empty($_POST["phone"])) {
        $phone_error = "Phone is required";
    } else {
        $phone = test_input($_POST["phone"]);
        //Check if phone is well-formed
        if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?]d{3}[\s-]?]d{4}$/i",$phone)) {
            $phone_error = "Invalid phone number";
        }
    }
    
    if (empty($_POST["url"])){
        $url_error = "";
    } else {
        $url = test_input($_POST["url"]);
        // Check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.:]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
            $url_error = "Invalid URL";
        }
    }
    */
    if (empty($_POST["subject"])) {
        $subject = "";
    } else {
        $subject = test_input($_POST["subject"]);
    }
    
    if (empty($_POST["message"])) {
        $message = "";
    } else {
        $message = test_input($_POST["message"]);
    }
    }
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    PHP:
    I am following a youtube video to get my contact form working so far we are testing the PHP to get it to print above the contact form which seems to be working. However there is an Array() error appear above it. I'm not too sure why? Any help would be appreciated.
     
    Solved! View solution.
    Web_Dev_Chris, Dec 26, 2018 IP
    SEMrush
  2. #2
    On line 3 in your PHP file you're telling it to print out your $_POST array. Try removing that, and see how it behaves.
     
    homemadejam, Dec 26, 2018 IP
  3. Web_Dev_Chris

    Web_Dev_Chris Active Member

    Messages:
    180
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    78
    #3
    That seemed to have fixed the array error. Thanks. However, if I would like to test it to print to the page is it suppose to have 'Array()'?

    Regards
    Chris
     
    Web_Dev_Chris, Dec 26, 2018 IP
  4. Web_Dev_Chris

    Web_Dev_Chris Active Member

    Messages:
    180
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    78
    #4
    Actually, nevermind. it is suppose to say that when I print. Thanks!

    Regards,
    Chris
     
    Web_Dev_Chris, Dec 26, 2018 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,975
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #5
    Where's your fieldset? Your label? Shouldn't the message be a textarea not an input? If you used a textarea instead of input and button instead of input for the submit, you could axe all the pointless classes.

    ... and FFS http://lmgtfy.com/?q=placeholder+is+not+a+label
     
    deathshadow, Dec 26, 2018 IP