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
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.
Link to comment
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
Share on other sites

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.