简体   繁体   English

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

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

i want to excecute php script with ajax or javascript from html form. 我想用html形式的ajax或javascript来优化php脚本。 I need receive result from php page to html page. 我需要从php页面到html页面接收结果。

My changepsw.php 我的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);

?>

if script fails, it returns Error setting password for $username. 如果脚本失败,则返回$ username的错误设置密码。 If success then php script return $user password set to $pass. 如果成功则php脚本将$ user密码设置为$ pass。

So i want to return answer from php page to html page with jquery/ajax. 所以我想通过jquery / ajax从php页面返回到html页面的答案。

My html form, from where I post data to my php script 我的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>

In your HTML page you can user AJAX post request and in php you must use the die method as follows: 在您的HTML页面中,您可以使用AJAX发布请求,在php中您必须使用die方法,如下所示:

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

In PHP code use as follows: die('1'); 在PHP代码中使用如下: die('1'); or die('0'); die('0'); or echo 'error occurs'; 或发生'回声'错误'; die; 死;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM