Jump to content


Photo

echo without asking


  • Please log in to reply
3 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 24 April 2006 - 09:41 PM

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
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";
}


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

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

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.
Im loving it ........

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 09:47 PM

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

#3 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 24 April 2006 - 10:01 PM

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.
Im loving it ........

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 24 April 2006 - 10:10 PM

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

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

or, replace the whole lot with this

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";
    }
}

If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users