newbie having trouble with php and forms

Discussion in 'PHP' started by demiancalixto, Sep 25, 2008.

  1. #1
    hello guys!
    i'm newbie to php and now trying to make some nice stuff.
    i'm developing a project to manage some data related to public health using php and xml.
    well, but going to the point, i have a page with forms like this:

    <html>
    <head>
    <title>VISA - Incluir Estabelecimento</title>
    </head>
    <body>
    <form action="salva.php" method="post">
    <p><b>Nome*: </b> <input type="text" name="nome" /></p>
    <p><b>Respons&aacute;vel*: </b> <input type="text" name="resp" /></p>
    <p><b>Endere&ccedil;o*: </b> <input type="text" name="ende" /></p>
    <p><b>Telefone: </b> <input type="text" name="tele" /></p>
    <p><b>Ramo de atividade: </b> <input type="text" name="ramo" /></p>
    <p><b>Emiss&atilde;o do &uacute;ltimo alvar&aacute;: </b> <input type="text" name="emis" /></p>
    <p><b>Vencimento do alvar&aacute;: </b> <input type="text" name="venc" /></p>
    <input type="hidden" value="OK" name="esco" />
    <p><input type="submit" value="Incluir dados" name="incl" /></p>
    </form>
    </body>
    </html>
    Code (markup):
    and this one submits data from the form to salva.php:

    <?php
    $tabela = new DOMDocument('1.0', 'utf8');
    	$tabela->formatOutput = true;
    	
    escreveNovo();
    $tabela->save('estab.xml');
    	
    $nomes = trim($_POST["nome"]);
    $resps = trim($_POST["resp"]);
    $endes = trim($_POST["ende"]);
    $teles = trim($_POST["tele"]);
    $ramos = trim($_POST["ramo"]);
    $emiss = trim($_POST["emis"]);
    $vencs = trim($_POST["venc"]);
    
    $spn = strlen($nomes);
    $spr = strlen($resps);
    $spe = strlen($endes);
    
    // ---------------------------------
    
    function escreveNovo() {
    if (empty($nomes) || empty($resps) || empty($endes)) {
    	echo("Preencha os campos obrigatórios! <a href=\"http://localhost/visa/inclui.php\">Voltar </a>");
    	exit;
    }
    else {	
    	header("Content-type: text/xml");
    	$indi = strpos($resps, " ");
    	$espa = $indi - 1;
    	$titu = substr($resps, 0, $espa);
    
    	$pri = $tabela->createElement("estabelecimento");
    	$tabela->appendChild($pri);
    	$sec = $tabela->createAttr("dono");
    	$pri->appendChild($sec);
    	$ter = $tabela->createElement("nome");
    	$pri->appendChild($ter);
    	$qua = $tabela->createElement("responsavel");
    	$pri->appendChild($qua);
    	$qui = $tabela->createElement("endereco");
    	$pri->appendChild($qui);
    	$sex = $tabela->createElement("telefone");
    	$pri->appendChild($sex);
    	$set = $tabela -> createElement("ramo");
    	$pri->appendChild($set);
    	$oit = $tabela -> createElement("emissao");
    	$pri->appendChild($oit);
    	$non = $tabela -> createElement("vencimento");
    	$pri->appendChild($non);
    
    	$utit = $tabela->createTextNode($titu);
    	$sec->appendChild($utit);
    	$enom = $tabela->createTextNode($nomes);
    	$ter->appendChild($enom);
    	$pser = $tabela->createTextNode($resps);
    	$qua->appendChild($pser);
    	$edne = $tabela->createTextNode($endes);
    	$qui->appendChild($edne);
    	$elet = $tabela->createTextNode($teles);
    	$sex->appendChild($elet);
    	$omar = $tabela->createTextNode($ramos);
    	$set->appendChild($omar);
    	$sime = $tabela->reateTextNode($emiss);
    	$oit->appendChild($sime);
    	$cnev = $tabela->createTextNode($vencs);
    	$non->appendChild($cnev);
    }
    }
    
    ?>
    Code (markup):
    i'm in trouble, because even when all text fields are filled, the script ends up to the first "if" clause in function escreveNovo(), and returns the message (which should appear only if one of the first three fields were empty).

    i'm really stuck with that. Another important thing is that i've already tried to echo the strings from the forms, and they are actually there, working perfectly!

    thanks in advance for the support!
     
    demiancalixto, Sep 25, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, I don't know, but when I look at it, how does your function know the POST values?? You don't pass them into the function's arguments, you don't call the $_POST superglobal, so I guess that's the issue. So either use glob inside the function, as a glob $nomes, $resps, .... , call the function after you assign these values from the form values or put all this :
    $nomes = trim($_POST["nome"]);
    $resps = trim($_POST["resp"]);
    $endes = trim($_POST["ende"]);
    $teles = trim($_POST["tele"]);
    $ramos = trim($_POST["ramo"]);
    $emiss = trim($_POST["emis"]);
    $vencs = trim($_POST["venc"]);

    $spn = strlen($nomes);
    $spr = strlen($resps);
    $spe = strlen($endes);

    inside the function.
     
    lp1051, Sep 25, 2008 IP
  3. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This variables should be declared either before calling escreveNovo() function...
    
    $nomes = trim($_POST["nome"]);
    $resps = trim($_POST["resp"]);
    $endes = trim($_POST["ende"]);
    $teles = trim($_POST["tele"]);
    $ramos = trim($_POST["ramo"]);
    $emiss = trim($_POST["emis"]);
    $vencs = trim($_POST["venc"]);
    
    $spn = strlen($nomes);
    $spr = strlen($resps);
    $spe = strlen($endes);
    
    escreveNovo();
    
    PHP:
    .. or declared inside of it, like this:

    
    function escreveNovo() {
    
    $nomes = trim($_POST["nome"]);
    $resps = trim($_POST["resp"]);
    $endes = trim($_POST["ende"]);
    $teles = trim($_POST["tele"]);
    $ramos = trim($_POST["ramo"]);
    $emiss = trim($_POST["emis"]);
    $vencs = trim($_POST["venc"]);
    
    $spn = strlen($nomes);
    $spr = strlen($resps);
    $spe = strlen($endes);
    
    // rest of function's code here
    }
    
    PHP:
     
    Ikki, Sep 25, 2008 IP