Jump to content

Archived

This topic is now archived and is closed to further replies.

avo

echo without asking

Recommended Posts

Hi all

can any one please explan what im doing incorrect please im not after code to solve it but an explanation into why .

my code [code]if ($_POST ['Signup']) {

mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error () );
mysql_select_db ($dbname) or die (mysql_error () );
$sql= "INSERT INTO members ( username ) VALUES ( '".$_POST['muser']."')";
mysql_query ($sql) or die ( mysql_error () );
mysql_close ();
}




if ($_POST ['muser'] !="" ) {
$frm_check_username ="good";
$frm_check = "good";
} else {
$frm_check_username ="bad";
$frm_check = "bad";
}

[/code]

All it is at the moment is a simple one line form contaning username when i press the form button its passed to the &_POST variable this then connects to my db

the if statement is linked to my username text box if this as some text in the box all ok but if not then frm_check = bad

the code echoed in my html is

[code]      <?php if ($frm_check == "bad") {
    echo "<td colspan=\"2\"><div align=\"center\">error</div></td>";
    } ?>[/code]

my question is why when my form loads it echos the above out and dont wait untill i have pressed the submit button hen not text as been entered into the box if text is in the box all works fine and error is removed untill irefesh the browser again.



Thank you in advance.

Share this post


Link to post
Share on other sites
Is your script in a file named "something.php"? If it's not, then the PHP interpreter is not being invoked and it will look like regular text to HTML, which displays it on the screen.

Ken

Share this post


Link to post
Share on other sites
HI thanks yes its in a file called admin_member.php

can you please explane a little more on what you mentioned

if it helps im running this on a local host apache am i missing soemthing in my ini file.
my db is working (sql)

thanks and appriciated.

Share this post


Link to post
Share on other sites
If I'm understanding you correctly, you are getting the "error" echoed before you have pressed submit?

If so, this is because you dont say anything about requiring the submit to be pressed in the if/else code where $frm_check is defined

You need to use one of the two following instead

[code]if($_POST['muser'] && $_POST['Signup']) {
    $frm_check_username ="good";
    $frm_check = "good";
}
elseif(!$_POST['muser'] && $_POST['Signup']) {
    $frm_check_username ="bad";
    $frm_check = "bad";
}[/code]

or, replace the whole lot with this

[code]if ($_POST['Signup']) {
    mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error () );
    mysql_select_db($dbname) or die (mysql_error () );
    $sql= "INSERT INTO members ( username ) VALUES ( '".$_POST['muser']."')";
    mysql_query($sql) or die( mysql_error () );
    mysql_close();
    
    if ($_POST['muser'] !="" ) {
        $frm_check_username ="good";
        $frm_check = "good";
    }
    else {
        $frm_check_username ="bad";
        $frm_check = "bad";
    }
}[/code]

Share this post


Link to post
Share on other sites

×

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.