Jump to content

Simple submission with validation


SkyRanger

Recommended Posts

Not sure what I am missing. When submitted gives white page with no submission. Error check is not showing anything.

 global $userdata, $current_user;

$tablename=kudos_table_name();

// Functions to filter user inputs
function filterkudoagent($field){
    // Sanitize user name
    $field = filter_var(trim($field), FILTER_SANITIZE_STRING);

    // Validate user name
    if(filter_var($field, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z-\s]+$/")))){
        return $field;
    } else {
        return FALSE;
    }
}

function filterkudocust($field){
    // Sanitize user name
    $field = filter_var(trim($field), FILTER_SANITIZE_STRING);

    // Validate user name
    if(filter_var($field, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z-\s]+$/")))){
        return $field;
    } else {
        return FALSE;
    }
}


function filterString($field){
    // Sanitize string
    $field = filter_var(trim($field), FILTER_SANITIZE_STRING);
    if(!empty($field)){
        return $field;
    } else{
        return FALSE;
    }
}

$kudoagentErr = $agentlocErr = $kudoclientErr = $kudocustErr = $kudomsgErr = $kudoadnErr = "";
$kudoagent = $agentloc = $kudoclient = $kudocust = $kudomsg = $kudoadn = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
    {

    if(empty($_POST["kudoagent"])){
        $kudoagentErr = "Please enter agent name.";
    } else{
        $kudoagent = filterkudoagent($_POST["kudoagent"]);
        if($kudoagent == FALSE){
            $kudoagentErr = "Please enter a valid agent name.";
        }
    }

    if(empty($_POST['agentloc'])){
        $agentlocErr = "You must select a location";
    }

    if(empty($_POST['kudoclient'])){
        $kudoclientErr = "You must select a queue";
    }

    if(empty($_POST["kudocust"])){
        $kudocustErr = "Please enter customer name.";
    } else{
        $kudocust = filterkudocust($_POST["kudocust"]);
        if($kudocust == FALSE){
            $kudocustErr = "Please enter a valid customer name.";
        }
    }

    if(empty($_POST["kudomsg"])){
        $kudomsgErr = "Please enter the kudos.";
    } else{
        $kudomsg = filterString($_POST["kudomsg"]);
        if($kudomsg == FALSE){
            $kudomsgErr = "Please enter a valid kudos.";
        }
    }

    if(empty($_POST["kudoadminname"])){
        $kudoadnErr = "Please enter the posters name.";
    } else{
        $kudoadn = filterkudocust($_POST["kudoadminname"]);
        if($kudoadn == FALSE){
            $kudoadnErr = "Please enter a valid posters name.";
        }
    }

    if (isset($_POST['kudoagentid']) && trim($_POST['kudoagentid']) != '') {
    $kudoagidErr = "Enter Agent ID or leave blank";
}
else {
    $kudoacctErr = "";
}

    if (isset($_POST['kudoacct']) && trim($_POST['kudoacct']) != '' && !ctype_digit($_POST['kudoacct'])) {
    $kudoacctErr = "Only numbers are permitted or leave blank";
}
else {
    $kudoacctErr = "";
}

     if(empty($kudoagentErr) && empty($kudocustErr) && empty($kudomsgErr) && empty($agentlocErr) && empty($kudoclientErr) && empty($kudoadnErr)){



     global $wpdb;
            $table = $tablename;
            $kudokey = randkey();
            $kudoposted = date("Y-m-d H:i:s");
            $data = array(
                'kudoid' => '',
                'kudomsg'    => $_POST['kudomsg'],
                'kudoagent' => $_POST['kudoagent'],
                'kudoagentid' => $_POST['kudoagentid'],
                'kudocust'    => $_POST['kudocust'],
                'kudoacct' => $_POST['kudoacct'],
                'kudoclient'    => $_POST['kudoclient'],
                'kudoloc' => $_POST['agentloc'],
                'kudoentry'    => $kudoposted,
                'kudoadmin'    => $_POST['kudoadminname'],
                'kudopic'     => $_POST['kudobanner'],
                'kudostatus'   => '1',
     			'kudokey'  => $kudokey,
            );
            $format = array(
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s'
            );
            $success=$wpdb->insert( $table, $data, $format );
            if($success){

        echo "<blockquote class=\"otro-blockquote\">";
        echo nl2br($_POST['kudomsg']);
        echo "<span>";
        echo "<b>Kudos for:</b> " .$_POST['kudoagent']. "  " .$_POST['kudoagentid']. ", ".$_POST['agentloc'];
  		echo "<br>By: ".$_POST['kudocust']. ", " . date("F j, Y g:i a", strtotime($kudoposted));
  		echo "<br/>" .$_POST['kudoacct'];
  		echo "<br/> Submitted By: " .$_POST['kudoadminname'];
        echo "</span></blockquote>";
        $urlparts = parse_url(home_url());
        $kudourl = $urlparts['host'];

         echo "<div class='success_msg'>Kudos Submitted</div>";

            }
    }

    } else {

?>   

	<h3>Add Kudos - No Notification Sent</h3>
    <b>* states required</b>

<form name="kudos_form" id="submitkudos" method="post" action="" enctype="multipart/form-data">
    <table class="addkudotable">
    <tr>
        <td>Agent Name *</td>
        <td>     </td>
        <td><input name="kudoagent" type="text" value="<?php echo $_POST['kudoagent']; ?>"/>
         <?php echo "<div class='note'>".$kudoagentErr."<br/>";?>
        </td>
    </tr>
    <tr>
        <td>Agent ID</td>
        <td></td>
        <td><input name="kudoagentid" type="text" value="<?php echo $_POST['kudoagentid']; ?>" />
        <?php echo "<div class='note'> </div>";?>
        </td>
    </tr>
    <tr>
        <td>Agent Location *</td>
        <td></td>
        <td><select name="agentloc" size="1">
    <option value="<?php echo $_POST['agentloc']; ?>"><?php echo $_POST['agentloc']; ?></option>
    <option></option>
    <option value="Amherst Center">Amherst Center</option>
    <option value="Charlottetown Center">Charlottetown Center</option>
    <option value="Kingston Center">Kingston Center</option>
    </select>
        <?php echo "<div class='note'>".$agentlocErr."</div>";?>
        </td>
    </tr>
    <tr>
        <td>Agent Queue *</td>
        <td></td>
        <td><select name="kudoclient" size="1">
    <option value="<?php echo $_POST['kudoclient']; ?>"><?php echo $_POST['kudoclient']; ?></option>
    <option value=""></option>
    <option value="AR">AR</option>
    <option value="Eastlink Tech Support">Eastlink Tech Support</option>
    <option value="PNI">PNI</option>
    </select>
        <?php echo "<div class='note'>".$kudoclientErr."</div>";?>
    </td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
        <tr>
        <td>Customer Name *</td>
        <td></td>
        <td><input name="kudocust" type="text" value="<?php echo $_POST['kudocust']; ?>"/>
        <?php echo "<div class='note'>".$kudocustErr."</div>";?>
        </td>
    </tr>
        <tr>
        <td>Customer Account #</td>
        <td></td>
        <td><input name="kudoacct" type="text" value="<?php echo $_POST['kudoacct']; ?>" />
        <?php echo "<div class='note'>".$kudoacctErr."</div>";?>
        </td>
    </tr>
        <tr>
        <td>Kudos *</td>
        <td></td>
        <td><textarea cols="50" name="kudomsg" rows="5"><?php echo $_POST['kudomsg']; ?></textarea>
        <?php echo "<div class='note'>".$kudomsgErr."</div>";?>
        </td>
    </tr>
        <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
        <tr>
        <td>Choose Image</td>
        <td></td>
        <td><select onchange="$('#imageToSwap').attr('src', this.options[this.selectedIndex].value);" name="kudobanner">
    <option value="<?php echo plugins_url(); ?>/img/kudos/1.jpeg" selected>Kudos 1</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/2.jpg">Kudos 2</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/3.png">Kudos 3</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/4.png">Kudos 4</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/5.png">Kudos 5</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/6.jpg">Kudos 6</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/7.jpg">Kudos 7</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/8.jpg">Kudos 8</option>
    <option value="<?php echo plugins_url(); ?>/kudos/img/kudos/9.png">Kudos 9</option>
</select></td>
    </tr>
        <tr>
        <td>Agent submitting Kudos</td>
        <td></td>
        <td><input name="kudoadminname" type="text" value="<?php echo $_POST['kudoadminname']; ?>">
         <?php echo "<div class='note'>".$kudoadnErr."</div>";?></td>
    </tr>
        <tr>
        <td> </td>
        <td><input type="hidden" name="action" value=""/></td>
        <td></td>
    </tr>
        <tr>
        <td><button type="submit" value="submit" class="aciformbutton" name="submit" />Submit Kudos</button>
        <td></td>
        <td><button type="reset" value="reset" class="aciformbutton">Start Over</button></td>
    </tr>

</table>
<input type="hidden" value="" name="kudoentry">
</form>
<script>

</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<?php
echo "<img id=\"imageToSwap\" class=\"profile\" src=" . plugins_url() . "/kudos/img/kudos/1.jpeg>";

}

 

Link to comment
Share on other sites

Are you sure you are configured to display errors?  Consider adding the following at the top of the page.

error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);


You also have calls to a bunch of functions which are not declared in your script.  Are they being imported elsewhere?

When I get stuck, I put little traps in the code (either echo, exit, or syslog) to pinpoint where the error  is. 

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.