点击链接执行PHP功能

时间:2016-04-20 11:27:19

标签: php

我想在点击链接或按钮时触发PHP功能。我该怎么做?我使用了javascript onclick功能,但有没有办法这样做?

1 个答案:

答案 0 :(得分:0)

最好选择jquery,ajax以json格式发送数据。像

<强> code.js

$(document).ready(function(){
    $("#submit").click(function(e){

        var status = $('form')[0].checkValidity();
        if(status){
            var formData = new FormData($('form')[0]);

            $.ajax({
                url: "code.php",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                dataType: "JSON",   

                success: function(json){
                    if(json.error){
                        alert(json.error_msg);
                        e.preventDefault();
                    }else{
                        alert("Data stored successfully!");
                    }
                },

                error: function(jqXHR, textStatus, errorThrown){
                    alert(errorThrown);
                }
            });
        }

    });
});

<强> code.php

<?php
    define('HOST','localhost');
    define('USER','***');
    define('PASS','***');
    define('DB','***');

    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

    if(!mysqli_connect_errno()){

        $error_flag = false;
        /*foreach($_POST as $value){
            if(empty($value)){
                $error_flag = true;
                break;
            }
        }*/

        if(!$error_flag){

            //receiving post parameters
             $sdn =$_POST['sdn'];




                                    // create a new user profile
                                    $sql = "INSERT INTO safety(sdn, created_at) VALUES ('$sdn',NOW())";
                                    if(mysqli_query($con,$sql)){
                                        $response["error"] = false;
                                        echo json_encode($response);

                                    }else{
                                        $response["error"] = true;
                                        $response["error_msg"] = "INSERT operation failed";
                                        echo json_encode($response);
                                    }

        }else{
            $response["error"] = true;
            $response["error_msg"] = "Few fields are missing";
            echo json_encode($response);
        }

    }else{
        $response["error"] = true;
        $response["error_msg"] = "Database connection failed";
        echo json_encode($response);
    }
?>

最后在里面使用jquery链接 的 code.html

<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src = "code.js"></script>

</head>

<body>
    <form id="form" name ="form" method = "POST" action="Next_page.html" class="wizard-big" autocomplete = "off">

<div class="col-sm-3 form-group">
<input type="text" placeholder="sdn" class="form-control"  id="sdn" name="sdn">
<div class="col-sm-12 form-group">
<input style="width:100%" type="submit" name = "submit" id = "submit" value = "Save and continue"  class="btn btn-success">
</div>
</div>
</form>
</body>
</html>