Stefan
Stefan

Reputation: 1

PHP: send post to div on same page instead of another page

I have a page called login.php loaded into a div on another page (index.php). login.php includes a form with action pointing to login.php

Once I press the submit button, the page will open either in a new tab (if no target is specified), or as the whole page (if target is _top, overwriting index.php). what I want is to have index.php open while login.php is loaded into it's div, and the post data to go to login.php which is inside the div. is that even possible?

LOGIN.PHP

<!DOCTYPE html>
<html>
<head>
    <link href="others.css" rel="stylesheet">
</head>
<body>
<?php
    require_once('funcs.php');
    if (!empty($_POST['user']) && !empty($_POST['pass'])) 
    {
        $username=cleanInput($_POST['user']);
        $password=cleanInput($_POST['pass']);
        if (login($username,$password)) { show_login_form("Invalid username or password!"); }
        else 
        {
            // login...
            echo "okay boss!";
        }
    }
    else { show_login_form(null); }
?>
</body>
</html>

FUNCS.PHP

<?php

function cleanInput($data)
{
    $data=trim($data);
    $data=htmlspecialchars($data,ENT_QUOTES, "UTF-8");
    return $data;
}

function show_login_form($err)
{
    echo '<h4>Enter username and password:</h4>';
    if (isset($err)) { echo "<span style='color:red'>Error: $err</span><br>"; }
    echo '
        <br><form method="post" action="login.php"> 
        username: <br><input name="user" type="text"><br> 
        password: <br><input name="pass" type="password"><br><br>
        <input id="button" type="submit" value="Login"></form>
    ';
}
?>

INDEX.PHP

 <!DOCTYPE html>
    <html>
    <head>
      <title>Dating Site</title>
      <link href="site.css" rel="stylesheet">
    </head>
    <body>
    <?php require_once ("header.php"); ?>
    <div id="mainframe" class="mainframe">
        <?php require_once ("home.php"); ?>
    </div>
    <?php require_once ("footer.php"); ?>

</body>
</html>

and to load a page into the "mainframe" div,I use this code:

function loadsel(page)
    {   
        if (page == 1)
            $("div#mainframe").load('login.php');
    }

Upvotes: 0

Views: 872

Answers (1)

Jithu R Jacob
Jithu R Jacob

Reputation: 368

What you need to do is use ajax to submit your form . Try this plugin and add the following code

   http://jquery.malsup.com/form/#ajaxSubmit

download the plugin

   function login(){
      $('#login_form').ajaxSubmit({

    target:'#output_login',
    url:'./php/login.php'


   });
  return false;

}

Upvotes: 1

Related Questions