在MYSQL中插入数据,并在单击按钮时打开对话框

时间:2014-02-06 08:03:42

标签: php jquery forms

我的details.php页面中涉及两种形式,即详细信息表单和对话框表单,如jsfiddle所示。

当用户点击“分配给某人”按钮时,我想使用MYSQL在数据库中插入“detail-form”的数据。对我来说这是一项艰巨的任务,因为它将包括打开对话框和插入数据。所以任何想法如何解雇那个Insert查询??

以下是我的代码:

<?php
   session_start();

   function validatedata()
   {
      ob_start();


// If the form was submitted, scrub the input (server-side validation)
// see below in the html for the client-side validation using jQuery
      $name= '';
      $clname = '';
      $clemail= '';

// collect all input and trim to remove leading and trailing whitespaces
      $name = trim($_POST['name']);
      $clname = trim($_POST['client_name']);
      $clemail= trim($_POST['client_email']);
      $errors = array();

      if (strlen($name) < 2 && (!filter_var($name, FILTER_VALIDATE_REGEXP,
      array("options"=>array("regexp"=>"/[a-zA-Z ]/")))) )
               array_push($errors, "Please enter a name. Name must contain at least 2 characters");

      if (strlen($clname) < 2 && (!filter_var($clname, FILTER_VALIDATE_REGEXP,
      array("options"=>array("regexp"=>"/[a-zA-Z ]/")))) )
               array_push($errors, "Please enter a name. Name must contain at least 2 characters");

      if (!filter_var($clemail, FILTER_VALIDATE_EMAIL))
               array_push($errors, "Please specify a valid email address");

// If no errors were found, proceed with storing the user input
      if (count($errors) == 0) 
      {
               array_push($errors, "No errors were found. Thanks!");
      }

//Prepare errors for output
      $output = '';
      foreach($errors as $val) {
               $output .= "<p class='output'>$val</p>";
          }

   }  


function insert_data()
{ 

$name= $_POST['name'];
$client_name= $_POST['client_name'];
$client_email= $_POST['client_email'];


$con=mysqli_connect("localhost","root","","my_db"); 
$sql = "INSERT INTO `Client_details`(`name`, `client_name`, `email`)  VALUES ('$name','$client_name','$client_email')";
    mysqli_query($con,$sql)or die(mysqli_error($con));


}


 if(isset($_POST['submit1']))
{
validatedata();
insert_data();
header("location:sections.php");
exit;
}


?>

1 个答案:

答案 0 :(得分:0)

点击分配按钮时,您可以触发ajax帖子;

$("#Assign").on("click", function() {
    $.ajax({
      type: "POST",
      url: your_url,
      data: $("#detail-form").serialize(),
      success: function(response) {
        //handle response
      }
    });
});

您可以在此处看到更新后的小提琴: http://jsfiddle.net/cubuzoa/qC4WR/3/