Jump to content

[SOLVED] another question for everyone who knows what there doing


Recommended Posts

i have a html page with a form and a php with a script. in the script i have a if/else statement. could i put javascript in the php script inside the if/else so it would display in a div on the html page after a submit button was clicked?

yes you can..

 

E.G..

<?

  if (whatever == lolol) {

    ?>

<script type="text/javascript">

lalalal

</script>

    <?

  }

  else {

    ?>

<script type="text/javascript">

OMG LOL!

</script>

    <?

  }

?>

check out this page... mylahstone.com    just enter any name and password.

i don't like the separate page it goes to for the error. and i don't want an alert box to pop up... i know how to do all that. i would like the error to appear on the same page.

ok

you don't need javascript

 

do this..

 

<?php
  if (your condition) {
    $message = "1 message";
  }
  else {
    $message = "another message";
  }
?>
<html>
  <head>
  </head>
  <body>
    <div><?php echo $message; ?></div>
  </body>
</html>

thanks for the help but now when i click on submit it takes me to the php page and that is empty. here is the code.

login.php

<?php
// log in success or failure

if ($count == 1) {
$message = "You are now logged in.";
    
} else {
$message = "This username is not registered. Pleaser register first.";

}

?> 

 

and my div code

index.html

<?php // this page is html not php
<div>
      <div align="center"><?php echo $message; ?></div>
</div>
?>

and my div code

index.html

<?php // this page is html not php
<div>
      <div align="center"><?php echo $message; ?></div>
</div>
?>

 

get rid of those <?php and ?> tags from around your divs.. and then your problem is solved..

i just put the php tags in there so the code was easier to read.

ok,,, here is all the code so far. check out the page if you would like.

http://mylahstone.com

the red text is where i'm looking to put the success/error message,, not in a seperate browser window.

// this is the php page with my form
<?php
<html>
<head>

<style>
.error { color: #ff0000; }
</style>
<!-- using javascript to make sure fields are not left empty -->

    <script type="text/javascript" language="javascript">

    function validate(form1)
    {
    var valid = true;

    if (form1.username.value)
    {
        document.getElementById('username_error').innerHTML = '';
    }
    else
    {
        document.getElementById('username_error').innerHTML = 'Please enter a username.';
        valid = false;
    }

    if (form1.password.value)
    {
        document.getElementById('password_error').innerHTML = '';
    }
    else
    {
        document.getElementById('password_error').innerHTML = 'Please enter a password.';
        valid = false;
    }
    return valid;
    }
    </script>
    
<style type="text/css">
    <!--
    body {
    background-color: #FFFFFF;
    }
    -->
    </style>

    <title></title>
    <style type="text/css">
    p.c3 {text-align: center}
    div.c2 {text-align: right}
    div.c1 {text-align: center}
    .style4 {color: #FF0000}
    </style>
</head>

<body>
    <p> </p>

    <p> </p>

    <p> </p>

    <p> </p>

    <table width="100%" height="36" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td height="36">
                <div class="c1">
                    <img src="pics/header.png" alt="header" width="433" height="37">
                </div>
            </td>
        </tr>
    </table>

    <p align="center">Hi, please log in.</p>
    <div>
      <div align="center" class="style4">I would like the message to display here instead of on new page.</div>
    </div>
<form action="login.php" method="post" name="form1" id="form1" onSubmit="return validate(this);">
<table width="100%" border="0" cellspacing="3" cellpadding="0">
            <tr>
                <td width="39%">
                    <div class="c2">
                        <label for="username">Username:</label>
                    </div>
                </td>

                <td width="61%"><input name="username" type="text" size="35" /> <span id="username_error" class="error"></td>
            </tr>

            <tr>
                <td>
                    <div class="c2">
                        <label for="password">Password:</label>
                    </div>
                </td>

                <td><input name="password" type="password" size="35" /> <span id="password_error" class="error"></td>
            </tr>

            <tr>
                <td> </td>

                <td><input type="submit" name="submit" id="submit" value="  Log in  "></td>
            </tr>
        </table>
    </form>

<p class="c3">Don't have a username?
    <br>
     Click here to register.</p>

<!-- using javascript  to set focus of the cursor on the username textfield -->
<script type="text/javascript" language="javascript">

    document.form1.username.focus();

</script>

</body>
</html>
?>

 

// this is the php with the script
<?php

$host = "-------------"; // host name
$dbusername = "--------------"; // mysql username
$dbpassword = "------------"; // mysql password
$db_name = "-----------"; // database name
$tbl = "-------------"; // table name

// connects to server and members databse.
mysql_connect("$host", "$dbusername", "$dbpassword") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");

// username/password changed to variables.
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM $tbl WHERE username='$username' and password='$password'";
$result = mysql_query($sql);

// create variable for success/failure message
$count = mysql_num_rows($result);

// log in success or failure
if ($count == "1") {
    echo "Thank you for logging in.";
} else {
    echo "This is not a registered username. Please register before logging in.";
}
?> 

oh then do the check @ the top of the php page..

 

if fail

 

header("location: index.php?error=".urlencode($message));

 

then in index.php

 

put

 

$_GET['error'] inside the div :)

i think we are getting closer.. after clicking the submit button,,, it keeps us on the index page but the error is displayed up top in the url box.

 

 

http://mylahstone.com/index.php?error=This+username+is+not+registered.+Pleaser+register+first.

 

I'll keep playing around with it.

nothing yet,,,,,i think i already tried putting the div inside a form and setting the form also... but that did not work.... i am out of ideas. i keep thinking it is something in this line of code

<?php
header("location: index.php?error=" . urlencode($message));
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.