开发者

Problems With Dynamic Form Using JavaScript and PHP

开发者 https://www.devze.com 2023-03-27 19:53 出处:网络
I\'m having trouble with these files, see if you guys can help me out :) I get an object required error..

I'm having trouble with these files, see if you guys can help me out :) I get an object required error..

index.php

<?php 
  //Start session
  session_start(); 
  //Require functions.php
  require("functions.php");
  //Require posts.php
  require("posts.php");
?>
<html>
<body>
<title>Log Into Booking System</title>
  <?php 
    //Call function to display login
    displayLogin();
  ?>
</body>
</html>

posts.php

<?php
if (isset($_POST['sublogin']))
{
    //Check that all fields were typed in and are not null
    if(!$_POST['user'] || !$_POST['pass'])
    {
        //Call function to display error
        printText("Please make sure all required fields are filled in.");
    }
    else
    {
        //Here is some database checking
    }
}
?>

functions.php

<?php   
function displayLogin()
{
    //Break php, then return to PHP
    ?> 
    <h1>Login</h1>
    <form action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
    <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
    <tr><td><div id='error'></div></td></tr>
    <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr> 
    </table>    
    </form>
    <?
}

function printText($txtmsg)
{
    ?>      
    <!-- Set up the displayError javascript function -->
    <script type="text/javascript">
        function displayError()
        {                   
            document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
        }
    </script>
    <!-- Call function to Display the error -->
    <script type="text/javascript">
        displayError();
    </script>
    <?
}
?>

I'm pretty sure

document.getElementById('error').innerHTML

Is causing me problems, i think it might have to do with how im calling it, like it dosent know what its looking for..

Thanks guys mu开发者_如何学编程ch appreciated,

Jenny

EDIT: Here is the paste of what jQuerybeast tried to do, http://pastebin.com/jj0RVMAd

SOLUTION: Who knew it was this easy? You MUST change the order of where posts.php is required as well as make sure you don't call a javascript function within functions.php because that will execute the javascript above the form and therefore giving a null object error.

Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
<title>Log Into Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php 
//Start session
session_start(); 
//Require functions.php
require("functions.php");
?>
</head>
<body>
<?php 
//Call function to display login
displayLogin(); 
//Require posts.php
require("posts.php");
?>
</body>
</html>

posts.php

<?php
//POST: Main login page
if (isset($_POST['sublogin']))
{
    //Display error if feilds are not filled in
    if(!$_POST['user'] || !$_POST['pass'])
    {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure all required fields are filled in.";</script>';
        return false;
    }
    else
    {
        //Some database minipulation here....
        //...
        if ($_POST['user'] != $databaseUsername)
        {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your username is correct.";</script>';
        return false;
        }
        elseif($_POST['pass'] != $databasePassword)
        {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your password is correct.";</script>';
        return false;           
        }
    }
    return true;
}

?>

functions.php

<?php


//Function: Display main.php login
function displayLogin()
{

    //Break php, then return to PHP
    ?> 
    <div style="float: left; padding: 10px; width:20%;">
    <fieldset>
    <legend>
    <h1>Login</h1>
    </legend>
    <form name = "mainlogin" action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
    <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
    <tr><td colspan="2" align="left"><input type="checkbox" name="remember">
    <font size="2">Remember username and password</td></tr> 
    <tr><td><div id="error"></div></tr><td>
    <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr> 
    </table>    
    </form>
    </fieldset>
    </div>

    <?php

}

?>


The first thing I notice is that the PHP-echoed string inside the Javascript function doesn't have quotation marks around it.

Change:

    function displayError()
    {                   
        document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
    }

To:

    function displayError()
    {                   
        document.getElementById('error').innerHTML = "<?echo $txtmsg;?>";
    }


I think the problem is caused from functions.php is because you open and close PHP the opposite way.

Instead of ?>  <?
Try: <?    ?>

Which is this:

function displayLogin()
{
    //Break php, then return to PHP
    <?
    <h1>Login</h1>
    <form action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
    <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
    <tr><td><div id='error'></div></td></tr>
    <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr> 
    </table>    
    </form>
    ?>
}


function printText($txtmsg)
{
    <?      
    <!-- Set up the displayError javascript function -->
    <script type="text/javascript">
        function displayError()
        {                   
            document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
        }
    </script>
    <!-- Call function to Display the error -->
    <script type="text/javascript">
        displayError();
    </script>
    ?>
}


change

 document.getElementById('error').innerHTML = <?echo $txtmsg;?>;

to

document.getElementById('error').innerHTML = '<?= $txtmsg; ?>';

or

document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';


Use this

document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';

and

It seems short tag is not enabled on your server. Please make sure. Or always use <?php ?> instead of <? ?>

0

精彩评论

暂无评论...
验证码 换一张
取 消