将数据从html表单发布到php脚本,并将结果返回到ajax/js/jquery


post data from html form to php script and return result to ajax/js/jquery

我想用ajax或javascript从html表单中优化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>

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

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

在PHP代码中,使用如下:CCD_ 1或CCD_echo"发生错误";死亡