1

i want to excecute php script with ajax or javascript from html form. I need receive result from php page to html page.

My 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. If success then php script return $user password set to $pass.

So i want to return answer from php page to html page with jquery/ajax.

My html form, from where I post data to my php script

<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>
2
  • Have you tried anything yet? Ajax and forms Commented Jul 27, 2016 at 8:58
  • no, i have no idea how to do that. I'm newbie to Ajax, Commented Jul 27, 2016 at 9:09

1 Answer 1

0

In your HTML page you can user AJAX post request and in php you must use the die method as follows:

$.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'); or die('0'); or echo 'error occurs'; die;

Sign up to request clarification or add additional context in comments.

1 Comment

could you explain more? i didn't get that :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.