在一种无效的方法中回显

时间:2013-01-03 16:57:27

标签: php echo

我使用http://www.webformgenerator.eu/生成表单并将代码下载到我现有的项目中。

现在按完提交按钮后,屏幕上没有任何反应。我调试并看到提交正在工作,它正在我编程时插入到db表中。

如果db insert成功,我想显示一条成功的消息。所以我只是回应了“成功”。但它没有显示在任何地方。

有什么不对?

代码段在这里。     

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

<?php
/**
 * Created with WebFormGenerator.eu
 * Powered by www.easyclick.ch
 */

$form = new ProcessForm();
$form->field_rules = array(
'house_id'=>'required',
'manethana_head'=>'required',
'sadasya_id'=>'required',
'relation_id'=>'required',
'name'=>'required',
'dob'=>'required',
'location_id'=>'required',
'marriage_id'=>'required',
'udyoga_id'=>'required',
'mobile'=>'',
'email'=>'email'
);
$form->validate();

class ProcessForm
{
public $field_rules;
public $error_messages;
public $fields;
private $error_list;
private $is_xhr;


function __construct()
{
    $this->error_messages = array(
        'required' => 'This field is required',
        'email' => 'Please enter a valid email address',
        'number' => 'Please enter a numeric value',
        'url' => 'Please enter a valid URL',
        'pattern' => 'Please correct this value',
        'min' => 'Please enter a value larger than the minimum value',
        'max' => 'Please enter a value smaller than the maximum value'
    );

    $this->field_rules = array();
    $this->error_list = '';
    $this->fields = $_POST;
    $this->is_xhr = $this->xhr();
}


function validate()
{
    if (!empty($this->fields))
    {
        //Validate each of the fields
        foreach ($this->field_rules as $field => $rules)
        {
            $rules = explode('|', $rules);

            foreach ($rules as $rule)
            {
                $result = null;

                if (isset($this->fields[$field]))
                {
                    $param = false;

                    if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
                    {
                        $rule = $match[1];
                        $param = $match[2];
                    }

                    $this->fields[$field] = $this->clean($this->fields[$field]);

                    //if the field is a checkbox group create string
                    if (is_array($this->fields[$field]))
                        $this->fields[$field] = implode(', ', $this->fields[$field]);

                    // Call the function that corresponds to the rule
                    if (!empty($rule))
                        $result = $this->$rule($this->fields[$field], $param);

                    // Handle errors
                    if ($result === false)
                        $this->set_error($field, $rule);
                }
            }
        }

        if (empty($this->error_list))
        {
         //   if ($this->is_xhr)
           //     echo json_encode(array('status' => 'success'));

            $this->process();

            echo "Success";
        }
        else
        {
            if ($this->is_xhr)
                echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list));
            else echo $this->error_list;
        }
    }
}


function process()
{
     /**
     * SUCCESS!!
     * There were no errors in the form. Insert your processing logic here (i.e. send an email, save to a
     * database etc.
     *
     * All of the submitted fields are available in the $this->fields variable.
     *
     * Example code to mail the results of the form:
     *
     * IMPORTANT - PLEASE READ:
     * 1. YOU MUST UNCOMMENT THE CODE FOR IT TO WORK.
     *    - This means removing the '//' in front of each line.
     *    - If you do not know what php comments are see here: http://php.net/manual/en/language.basic-syntax.comments.php
     *
     * 2. YOU CAN ENTER ANY EMAIL ADDRESS IN THE $from VARIABLE.
     *    - This is the address that will show in the From column in your mail application.
     *    - If your form contains an email field, and you want to use that value as the $from variable, you can set $from = $this->fields['name of your email field'];
     *    - As stated in the description on codecanyon, this code does not mail attachments. Google 'php html email attachments' for information on how to do this
     */
     // $msg = "Form Contents: \n\n";
     // foreach($this->fields as $key => $field)
     //       $msg .= "$key :  $field \n";
     // $to = 'emailaddress@domain.com';
     // $subject = 'Form Submission';
     // $from = 'emailaddress@domain.com';
     // mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");

    foreach($this->fields as $key => $field)
    {
        if ($key == 'house_id')
            $mane_id = $field; 
        elseif ($key == 'relation_id')
            $relation_id = $field; 
        elseif ($key == 'name')
            $name = $field; 
        elseif ($key == 'dob')
            $dob = $field;
        elseif ($key == 'blood_id')
            $blood_id = $field; 
        elseif ($key == 'mantra_id')
            $mantra_id = $field; 
        elseif ($key == 'location_id')
            $location_id = $field; 
        elseif ($key == 'marriage_id')
            $marriage_id = $field; 
        elseif ($key == 'udyoga_id')
            $udyoga_id = $field; 
        elseif ($key == 'mobile')
            $mobile = $field;
        elseif ($key == 'email')
            $email = $field;        
    }

    //Begin date, End date, Last changed by
    $enddate = '39991231';
    $begdate = date("Ymd");
    $last_changed_by = $_SESSION['user'];               

    // Get internal number for house
    $max_hse_int_sql = mysql_query("SELECT mane_id FROM kd_mane WHERE mane_id_external = $mane_id LIMIT 1") or die(mysql_error());
    $row_int_hse = mysql_fetch_row($max_hse_int_sql);
    $mane_id_internal = $row_int[0];
    $mane_id_internal = 00000;

    // Get the next available internal number
    $max_int_sql = mysql_query("SELECT MAX(sadasya_id) FROM kd_sadasya") or die(mysql_error());
    $row_int = mysql_fetch_row($max_int_sql);
    $sadasya_id = $row_int[0]+1;

    // Get the next available external number
    $max_sql = mysql_query("SELECT MAX(sadasya_id_external) FROM kd_sadasya WHERE mane_id_external = $mane_id") or die(mysql_error());
    $row = mysql_fetch_row($max_sql);
    if ($row[0]>0)
    {
        $sadasya_id_external = $row[0]+1;
    }
    else //first member in the house
    {
        $sadasya_id_external = $mane_id*100+1; //If mane id is 123405, first house in it will be 12340501
    }       

    // Linga_id from relationship
    $male_relationship_array = array(11, 13, 16, 18, 21, 23, 24, 26); //all males
    if (in_array($relation_id, $male_relationship_array))  
        $linga_id = '11';
    else
        $linga_id = '12';

    $query_string = "INSERT INTO `kd_sadasya` 
    (mane_id, sadasya_id, begin_date, end_date, mane_id_external, sadasya_id_external, 
    sambandha_id, sadasya_name, dob, rakta_id, vasa_id, linga_id, vivaha_id, 
    udyoga_id, mantra_id, dooravani, emailid, last_changed_by)
    VALUES
    ('$mane_id_internal', '$sadasya_id', '$begdate', '$enddate', '$mane_id', '$sadasya_id_external', 
    '$relation_id', '$name', '$dob', '$blood_id', '$location_id', '$linga_id', '$marriage_id',
    '$udyoga_id', '$mantra_id', '$mobile', '$email', '$last_changed_by')";

    mysql_query($query_string);

    $error = mysql_error();

    echo("Success");


} 



function set_error($field, $rule)
{
    if ($this->is_xhr)
    {
        $this->error_list[$field] = $this->error_messages[$rule];
    }
    else $this->error_list .= "<div class='error'>$field: " . $this->error_messages[$rule] . "</div>";
}

function xhr()
{
    return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
}


/** Validation Functions */
function required($str, $val = false)
{

    if (!is_array($str))
    {
        $str = trim($str);
        return ($str == '') ? false : true;
    }
    else
    {
        return (!empty($str));
    }
}


function email($str)
{
    return (!preg_match("/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $str)) ? false : true;
}


function number($str)
{
    return (!is_numeric($str)) ? false : true;
}


function min($str, $val)
{
    return ($str >= $val) ? true : false;
}


function max($str, $val)
{
    return ($str <= $val) ? true : false;
}


function pattern($str, $pattern)
{
    return (!preg_match($pattern, $str)) ? false : true;
}


function clean($str)
{
    $str = is_array($str) ? array_map(array("ProcessForm", 'clean'), $str) : str_replace('\\', '\\\\', strip_tags(trim(htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) : $str), ENT_QUOTES))));
    return $str;
    }
}


?>

编辑: 我评论下面的一行,它开始工作。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

这似乎是一个标准的脚本。它有错误吗? 我的控制台说

  

未使用的JSON无效:

在上面的js文件中。

1 个答案:

答案 0 :(得分:1)

正如您所提到的那样,数据正在按预期插入数据库,然后很可能

$this->process();

在此时停止进一步执行,可能有任何die命令或返回。只是为了调试你的

    echo "Success";

上面

$this->process();

修改

我注意到您在 else 部分中回显了JSON编码的字符串。也许您的内容类型设置为那样,这就是为什么您没有看到成功。尝试在json中编码成功消息,就像在评论中的行中一样