Jump to content

echo without asking


avo

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.
Link to comment
https://forums.phpfreaks.com/topic/8304-echo-without-asking/
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]
Link to comment
https://forums.phpfreaks.com/topic/8304-echo-without-asking/#findComment-30291
Share on other sites

Archived

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

×
×
  • 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.