Jump to content

problem with isset()


yona_ts

Recommended Posts

hello to every one. i am making this little application in php/mysql and am facing a little problem with function isset().
i have a registration form and the registration form sends the data at this php script.

Take a look at the code below.

[code]
<?php
  include "config.inc.php";
  include("class.FastTemplate.php3");

  $con=mysql_connect($dbServer, $dbUser, $dbPass)
  or exit("Cannot conect to database");

  if(isset($_POST["change"])){
      $tpl=new FastTemplate(".");
      $tpl->define(array("Emp_unsuccess" => "emp_unsuccess.tpl"));
      $tpl->assign(array("{COMP}" =>$_POST["company"],
                        "{ADDR}" => $_POST["address"],
        "{CITY}" =>$_POST["city"],
        "{ZIP}" =>$_POST["zip"],
        "{CONT}" =>$_POST["contact"],
        "{PHONE}" =>$_POST["phone"],
        "{FAX}" =>$_POST["fax"],
        "{MAIL}" =>$_POST["email"],
        "{USERNAME}"=>$_POST["user"],
        "{PASSWORD}"=>$_POST["password"]));

      if($_POST["field"]=="0"){
        $tpl->assign("{FIELD}","Mathematics");
        }
      if($_POST["field"]=="1"){
        $tpl->assign("{FIELD}","Sciences");
        }
      if($_POST["field"]=="2"){
        $tpl->assign("{FIELD}","Social Sciences");
        }
      if($_POST["field"]=="3"){
        $tpl->assign("{FIELD}","Computer Sciences");
        }
      if($_POST["field"]=="4"){
        $tpl->assign("{FIELD}","History");
        }
      if($_POST["field"]=="5"){
        $tpl->assign("{FIELD}","Arts");
        }
      if($_POST["field"]=="6"){
        $tpl->assign("{FIELD}","Foreign Languages");
        }

      if($_POST["material"]=="0"){
        $tpl->assign("{FORM}",".pdf");
        }
      if($_POST["material"]=="1"){
        $tpl->assign("{FORM}",".html");
        }
      if($_POST["material"]=="2"){
        $tpl->assign("{FORM}",".exe");
        }
      if($_POST["material"]=="3"){
        $tpl->assign("{FORM}",".mp3");
        }
      if($_POST["material"]=="4"){
        $tpl->assign("{FORM}",".wma");
        }
      if($_POST["material"]=="5"){
        $tpl->assign("{FORM}",".mpeg");
        }
      if($_POST["material"]=="6"){
        $tpl->assign("{FORM}",".zip");
        }

      $tpl->assign("{TITLE}","Registration unsuccessful");
      $tpl->parse("CONTENT","Emp_unsuccess");
      $tpl->FastPrint();
    }
?>
[/code]

wirthout the if statement the script is fine but as soon as i use it a blank page appears on the browser.
any ideas?
thnx in advance!
Link to comment
Share on other sites

[quote author=taith link=topic=121781.msg501358#msg501358 date=1168443457]
yes... with forms, when you press submit, it sends every field as $_POST, as is, isset() will always return true, you'd want to use !empty()...
[/quote]

Actually, isset() is the appropriate check to use to see whether or not checkboxes have been selected (and radio buttons if you don't have a default value). Also, if you're simply checking to see whether or not the form has been submitted, isset() on any of your submit, hidden, text or other default value fields would be just fine.
Link to comment
Share on other sites

maybe i shoud post the hole script...
[code]
<?php
  include "config.inc.php";
  include("class.FastTemplate.php3");

  $con=mysql_connect($dbServer, $dbUser, $dbPass)
  or exit("Cannot conect to database");

  if(isset($_POST["change"])){

    $sql1="SELECT * FROM provider WHERE provider_name='".$_POST["user"]."'";
    $res1=mysql_db_query($dbDatabase,$sql1);


    if($res1 && mysql_num_rows($res1))
    {
      $tpl=new FastTemplate(".");
      $tpl->define(array("Emp_unsuccess" => "emp_unsuccess.tpl"));
      $tpl->assign(array("{COMP}" =>$_POST["company"],
                        "{ADDR}" => $_POST["address"],
        "{CITY}" =>$_POST["city"],
        "{ZIP}" =>$_POST["zip"],
        "{CONT}" =>$_POST["contact"],
        "{PHONE}" =>$_POST["phone"],
        "{FAX}" =>$_POST["fax"],
        "{MAIL}" =>$_POST["email"],
        "{USERNAME}"=>$_POST["user"],
        "{PASSWORD}"=>$_POST["password"]));

      if($_POST["field"]=="0"){
        $tpl->assign("{FIELD}","Mathematics");
        }
      if($_POST["field"]=="1"){
        $tpl->assign("{FIELD}","Sciences");
        }
      if($_POST["field"]=="2"){
        $tpl->assign("{FIELD}","Social Sciences");
        }
      if($_POST["field"]=="3"){
        $tpl->assign("{FIELD}","Computer Sciences");
        }
      if($_POST["field"]=="4"){
        $tpl->assign("{FIELD}","History");
        }
      if($_POST["field"]=="5"){
        $tpl->assign("{FIELD}","Arts");
        }
      if($_POST["field"]=="6"){
        $tpl->assign("{FIELD}","Foreign Languages");
        }

      if($_POST["material"]=="0"){
        $tpl->assign("{FORM}",".pdf");
        }
      if($_POST["material"]=="1"){
        $tpl->assign("{FORM}",".html");
        }
      if($_POST["material"]=="2"){
        $tpl->assign("{FORM}",".exe");
        }
      if($_POST["material"]=="3"){
        $tpl->assign("{FORM}",".mp3");
        }
      if($_POST["material"]=="4"){
        $tpl->assign("{FORM}",".wma");
        }
      if($_POST["material"]=="5"){
        $tpl->assign("{FORM}",".mpeg");
        }
      if($_POST["material"]=="6"){
        $tpl->assign("{FORM}",".zip");
        }
      $tpl->assign("{TITLE}","Registration unsuccessful");
      $tpl->parse("CONTENT","Emp_unsuccess");
      $tpl->FastPrint();
    }


    else{
    // insert data into provider
    $sql2="INSERT INTO provider VALUES
      ('".$_POST["user"]."',
'".$POST["password"]."',
'".$_POST["company"]."',.
'".$_POST["field"]."',
'".$_POST["material"]."',
'".$_POST["address"]."',
'".$_POST["city"]."',
'".$_POST["zip"]."',
'".$_POST["phone"]."',
'".$_POST["fax"]."',
'".$_POST["email"]."',
'".$_POST["contact"]."')";

    //-----------------------------------------
    $res2=mysql_db_query($dbDatabase,$sql2);
     
      $tpl=new FastTemplate(".");
      $tpl->define(array("Emp_success" => "emp_success.tpl"));
      $tpl->assign(array("{COMP}" =>$_POST["company"],
                        "{ADDR}" => $_POST["address"],
        "{CITY}" =>$_POST["city"],
        "{ZIP}" =>$_POST["zip"],
        "{CONT}" =>$_POST["contact"],
        "{PHONE}" =>$_POST["phone"],
        "{FAX}" =>$_POST["fax"],
        "{MAIL}" =>$_POST["email"],
        "{FIELD}" =>$_POST["field"],
        "{FORM}" =>$_POST["material"],
        "{USERNAME}"=>$_POST["user"],
        "{PASSWORD}"=>$_POST["password"]));   

      $tpl->assign("{TITLE}","Registration successful");
      $tpl->parse("CONTENT","Emp_success");
      $tpl->FastPrint();
 
    }
  }
?>

[/code]
Link to comment
Share on other sites

[quote author=taith link=topic=121781.msg501368#msg501368 date=1168444589]
true... isset() will check if the form was submitted, but !empty() is a much safer way of checking to see if a field has value...
[/quote]

As I said, though, this will cause an error with any form elements that don't have a default value (keeping in mind that [b]all[/b] text fields default to an empty string). Therefore, for certain input types, if you're going to have clean code, you've got to use a balance of isset() and empty(). One is not [i]safer[/i] than the other, it's just a matter of learning to use each for which it was intended.
Link to comment
Share on other sites

I think the reason you're getting a blank screen is that you process your form and everything all [b]within the submission check[/b]. Therefore, when you first hit your page, your form has not been submitted, and so nothing is displayed. You need to run your [b]processing only[/b] within the confines of your if statement. Your page should be displayed outside of those confines so that you can see the form [i]even when it hasn't been submitted yet[/i].
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.