将数据从html格式发布到php脚本并将结果返回给ajax / js / jquery

时间:2016-07-27 08:53:08

标签: javascript php jquery html ajax

我想用html表单中的ajax或javascript来执行php脚本。我需要从php页面到html页面接收结果。

我的changepsw.php

<?php

//Change a password for a User via command line, through the API.

//download the following file to the same directory:
//http://files.directadmin.com/services/all/httpsocket/httpsocket.php
$system = $_POST['system'];
                                    $db = $_POST['db'];
                                    $ftp = $_POST['ftp'];
                                    $id = $_GET['id'];
                                    $psw = $_POST['userpw'];
                                $queryda = "SELECT * FROM paugos where id = '$id'"; //You don't need a ; like you do in SQL
$resultda = mysql_query($queryda);
$rowda = mysql_fetch_array($resultda);

if($system == "" or $system == "no" or $system !== "yes"){
    $system = "no";
}   
if($db == "" or $db == "no" or $db !== "yes"){
    $db = "no";
}   
if($ftp == "" or $ftp == "no" or $ftp !== "yes"){
    $ftp = "no";
}                               
$server_ip="127.0.0.1";
$server_login="admin";
$server_pass="kandon";
$server_ssl="N";

$username = $rowda['luser'];
$pass= $psw;

echo "changing password for user $username\n";

include 'httpsocket.php';

$sock = new HTTPSocket;
if ($server_ssl == 'Y')
{
    $sock->connect("ssl://".$server_ip, 2222);
}
else
{ 
    $sock->connect($server_ip, 2222);
}

$sock->set_login($server_login,$server_pass);
$sock->set_method('POST');

$sock->query('/CMD_API_USER_PASSWD',
    array(
        'username' => $username,
        'passwd' => $pass,
        'passwd2' => $pass,
        'options' => 'yes',
        'system' => $system,
        'ftp' => $ftp,
        'database' => $db,
    ));

$result = $sock->fetch_parsed_body();

if ($result['error'] != "0")
{
    echo "\n*****\n";
    echo "Error setting password for $username:\n";
    echo "  ".$result['text']."\n";
    echo "  ".$result['details']."\n";
}
else
{
    mysql_query("UPDATE paugos SET lpass='$pass' WHERE id='$id'");
    //echo "<script type='text/javascript'> document.location = 'control?id=$id&successpw=1'; </script>";
    //header("Location: control?id=1&successpw=1");
    echo "$user password set to $pass\n";
}

exit(0);

?>

如果脚本失败,则返回 设置$ username的密码时出错。如果成功则php脚本将$ user密码设置为$ pass。

所以我想通过jquery / ajax将php页面的答案返回到html页面。

我的html表单,从我发布数据到我的php脚本

<form action="changepsw.php?id=<?=$id;?>" method="post" role="form">

                                       <label for="disabledSelect">Directadmin account</label>
                                                <input name="usern" class="form-control" style="width:220px;" type="text" placeholder="<?=$luser;?>" disabled>

                                        <div class="form-group">
                                            <label>New password</label>
                                            <input name="userpw" class="form-control" style="width:220px;" placeholder="Enter new password">
                                        </div>


                                        <div class="form-group">
                                            <label>Change password for:</label>
                                            <div class="checkbox">
                                                <label>
                                                    <input type="checkbox" name="system" value="yes">Directadmin
                                                </label>
                                            </div>
                                            <div class="checkbox">
                                                <label>
                                                    <input type="checkbox" name="ftp" value="yes">FTP
                                                </label>
                                            </div>
                                            <div class="checkbox">
                                                <label>
                                                    <input type="checkbox" name="dabatase" value="yes">MySQL
                                                </label>
                                            </div>
                                        </div>


                                        <button type="submit" id="col" class="btn btn-default">Submit Button</button>
                                        <button type="reset" class="btn btn-default">Reset Button</button>
                                    </form>

1 个答案:

答案 0 :(得分:0)

在您的HTML页面中,您可以使用AJAX发布请求,在php中,您必须使用die方法,如下所示:

$.post('url',{parameters},function(data){
if(data==='1'){
 alert('Done');
}else if(data==='0'){
 alert('Error');
}else{
 alert(data);
}
});

在PHP代码中使用如下: die('1');die('0');或 echo'错误发生'; 死;