Jump to content

cURL validation on php form


jsgsg

Recommended Posts

I am trying to implement a validation process within the php wrapper for the new form.

The automatically gets submitted and completely ignores the validation process. Is there any reason why?

 

<?php

//Initialize the $query_string variable for later use
$query_string = "";
 
//If there are POST variables
if ($_POST) {
 
//Initialize the $kv array for later use
$kv = array();
 
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $key => $value) {
 
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key)."=".stripslashes($value);

 
}


//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
 
//The original form action URL from Step 2 
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
 
//Open cURL connection
$ch = curl_init();


if(isset($_POST['submit'])){

    if($_POST['first_name'] == ""){
        $error="Please enter a topic<br>";
    }

    if($_POST['last_name'] == ""){
        $error="Please enter a topic<br>";
    }

    if($_POST['phone'] == ""){
        $error="Please enter a topic<br>"; 
    }

    if($_POST['email'] == ""){
        $error="Please enter a topic<br>";
    }

    if($_POST['company'] == ""){
        $error="Please enter a topic<br>";
    }

    if(isset($error)){
        echo $error;
    }else{
        // Both fields have content in

    }
}
 
//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
 
//Set some settings that make it all work 
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
 
//close cURL connection
curl_close($ch);

?>

 

Link to comment
Share on other sites

isset($_POST['submit']) is your main condition to trigger the validation block.... I'm guessing there is nothing called 'submit' being sent over...

 

Try changing isset($_POST['submit']) to !empty($_POST) and try again. If it works, you html is missing the 'submit' element.

Edited by WebStyles
Link to comment
Share on other sites

You are calling your cURL stuff regardless of the result of your validation. You need to move the curl stuff into the else block of your if (isset($error)) branch

 

if (isset($_POST['submit'])){
    //your validations
    if (isset($error)){
       //display error
    }
    else {
       //your cURL stuff
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.