Page 1 of 2

Problem with form

Posted: Wed Apr 23, 2008 2:39 pm
by DeFacto
Hello all
i have a problem with form, when it is submited i get empty lines in my database.

Code: Select all

 
<?php
if(isset($_POST["Submit"])) {
    check_form();
} else {
    show_form();
}
 
function check_email_address($email) {
 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
 
    return false;
  }
 
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
      return false;
    }
  }
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { 
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; 
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
 
 
function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;
$error['vardas'] = false;
    if($_POST["vardas"]=="") {
        $error['vardas'] = true;
         $print_again = true;
    }
    $error['pavarde'] = false;
    if($_POST["pavarde"]=="") {
        $error['pavarde'] = true;
         $print_again = true;
    }
    $error['adresas'] = false;
    if($_POST["adresas"]=="") {
        $error['adresas'] = true;
         $print_again = true;
    }
    $error['miestas'] = false;
    if($_POST["miestas"]=="") {
        $error['miestas'] = true;
         $print_again = true;
    }
    $error['pastas'] = false;
    if($_POST["pastas"]=="") {
        $error['pastas'] = true;
         $print_again = true;
    }
    if(!check_email_address($_POST['el_pastas'])) {
        $error['el_pastas'] = true;
         $print_again = true;
    }
     if($print_again) {
         show_form();
 
 
       } else {
        show_form();
          
       header('Location: DB/control/insert.php'); 
       }
 
}?>
 

Code: Select all

 
<body>
<?
function error_bool($error, $field) {
         if($error[$field]) {
             print("<td style=color:red>");
         }
       // else {
           // print("<td>");
        //}
    }
    function error_text($error) {
         if($error) {
             echo '<font style="color:#FF0000" size="2px">Neteisingai užpildyt? lauk? pavadinimai pažym?ti raudonai &nbsp;</font>';
         }
       else {
           echo "";
       }
    }
 
function show_form() {
global $HTTP_POST_VARS, $print_again, $error;}
?>
<form id="form1" name="form1" method="post" action="" align="left">
  <label "<?php error_bool($error, "vardas"); ?>">Vardas
  <input style="margin-top:45px" type="text" name="vardas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["vardas"];}
      else {echo "";} ; ?>">
  </label><br/>
  <label "<?php error_bool($error, "pavarde"); ?>">Pavard?
  <input style="margin-top:10px" type="text" name="pavarde" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["pavarde"];}
       ; ?>">
  </label><br/>
  <label "<?php error_bool($error, "adresas"); ?>">Adresas
  <input style="margin-top:10px" type="text" name="adresas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["adresas"];}
       ?>">
  </label>
  <br/>
  <label "<?php error_bool($error, "miestas"); ?>">Miestas
  <input style="margin-top:10px" type="text" name="miestas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["miestas"];}
      else {echo "";}  ?>">
  </label><br/>
  <label "<?php error_bool($error, "pastas"); ?>">Pašto indeksas
  <input style="margin-top:10px" type="text" name="pastas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["pastas"];}
      else {echo "";}  ?>">
  </label><br/>
  <label "<?php error_bool($error, "el_pastas"); ?>">El. paštas
  <input style="margin-top:10px" type="text" name="el_pastas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["el_pastas"];}
      else {echo "";}  ?>">
   </label><br/><?php error_text($error); $vardas=$_POST['vardas']; ?>
  <input style="margin-top:10px" name="Submit" type="submit" value="Patvirtinti" />
  <input name="Valyti laukus" type="reset" value="Valyti laukus" />
</form>
 

Re: Problem with form

Posted: Wed Apr 23, 2008 8:51 pm
by califdon
If your problem is that you are inserting blank records in your database, it would help if you show us your insert.php script, since that is what interacts with the database.

Re: Problem with form

Posted: Thu Apr 24, 2008 9:26 am
by DeFacto
This is insert.php code.

Code: Select all

 
<?php header("Content-Type: text/html; charset=utf-8");
include '../config/config.php';
include '../config/opendb.php';
mysql_query("SET NAMES 'utf8'");
$a=$_POST['vardas'];
$query="INSERT INTO klientai (vardas, pavarde, adresas, miestas, pastas, el_pastas)
VALUES
('$_POST[vardas]','$_POST[pavarde]','$_POST[adresas]','$_POST[miestas]','$_POST[pastas]','$_POST[el_pastas]')";
mysql_query($query)or die ('Error, insert query failed');
//echo "$a";
//echo "pavyko";
include'../config/closedb.php';
header('Location: ../../katalogas1.php');
?>
 
Everything was ok till i made that upgrade that checks form for empty inputs, after that when i fill inputs i get blank records in my database.

Re: Problem with form

Posted: Thu Apr 24, 2008 9:31 am
by andym01480
Put curly brackets {} around each of the $_POST variables in the query eg {$_POST['pastas']}

also echo the query before the mysql_query call just to check what is happenning

Re: Problem with form

Posted: Thu Apr 24, 2008 9:35 am
by aceconcepts
if you're checking whether a variable contains a value, I prefer to use empty()

Code: Select all

 
if(empty($_POST["pavarde"]))
 

Re: Problem with form

Posted: Thu Apr 24, 2008 9:59 am
by DeFacto
echo of query in insert.php, looks like empty fields comes from katalogas1.php.

Code: Select all

 
INSERT INTO klientai (vardas, pavarde, adresas, miestas, pastas, el_pastas) VALUES ('','','','','','')
 

Re: Problem with form

Posted: Thu Apr 24, 2008 10:04 am
by aceconcepts
Remove the last semi-colons ";" after your else { } statement in the value"" of your input fields.

Re: Problem with form

Posted: Thu Apr 24, 2008 10:12 am
by DeFacto
Same.

Re: Problem with form

Posted: Thu Apr 24, 2008 10:22 am
by aceconcepts
You're either losing the $_POST array from

Code: Select all

 
header('Location: DB/control/insert.php');
 
or you're not using the showForm() properly.

Re: Problem with form

Posted: Thu Apr 24, 2008 10:23 am
by DeFacto
how could i change it?

Re: Problem with form

Posted: Thu Apr 24, 2008 10:28 am
by andym01480
Using header() to call the db insert will loose the form $_POST values - it'll need to be an include!

Re: Problem with form

Posted: Thu Apr 24, 2008 10:30 am
by aceconcepts
I think that when you request a new page i.e.

Code: Select all

 
header('Location: DB/control/insert.php');
 
you lose the previously posted array - correct me if i'm wrong.

So maybe you could use an include for your insert or simply create your insert as a function.

Re: Problem with form

Posted: Thu Apr 24, 2008 10:39 am
by DeFacto
do you mean i should change

Code: Select all

 
header('Location: DB/control/insert.php');
 
with this?

Code: Select all

 
include "DB/control/insert.php";
 

Re: Problem with form

Posted: Thu Apr 24, 2008 10:46 am
by onion2k
aceconcepts wrote:if you're checking whether a variable contains a value, I prefer to use empty()

Code: Select all

 
if(empty($_POST["pavarde"]))
 
I use empty() too, but you need to be careful with it. Think about what will happen if the user needs to insert "0" (zero) as a string into a field. empty() will reject it because it's a bit stupid.

Re: Problem with form

Posted: Thu Apr 24, 2008 10:53 am
by DeFacto
DeFacto wrote:do you mean i should change

Code: Select all

 
header('Location: DB/control/insert.php');
 
with this?

Code: Select all

 
include "DB/control/insert.php";
 
Thanks a lot everybody, that helpped to solve my problem :D