problema nella implementare una funzione con ritorno

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
43
Massa, Italy
vorrei fare una funzione tipo questa:
PHP:
function _isset($var)
{
  $r = (isset($var)) ? trim (($var)): '';
  return $r;
}
per non scrivere tutte le volte:
PHP:
$username= (isset($_POST["username"])) ? trim (($_POST["username"])): ''
e far cosi:

PHP:
$username = _isset($_POST["username"]);

ma non me lo fa fare mi da sempre Undefined variable.
come mai.?

vi ringrazio molto, e buona serata.
 
ciao
sei sicuro che non funzi?
PHP:
<?php
function _isset($var){
  $r = (isset($var)) ? trim (($var)): '';
  return $r;
}
echo "<pre>";
$username = _isset($_POST["username"]); 
var_dump($username);
$_POST["username"]="pinco";
$username = _isset($_POST["username"]); 
var_dump($username);
echo "<pre>";
?>
e questo è l'output dei due var_dump
string(0) ""
string(5) "pinco"
 
a me qua non funziona .. mi da sempre il notice di prima:

leggi il commento dove c'è : // Qui la parte dove voglio inserire la funzione.


idee?

grazie mille.


PHP:
<?php session_start(); ?> 
<?php
require_once("lib/func.php");
require_once("layout/template.php");
$title = "Contact";
layout_header($title);
?>
<div align="center">
<?php

if(isset($_POST['submit'])) {
 
      $errors = array();
    
      if($_POST['name'] == "") {
         $errors[] = 'The name field is empty';
      }
      if($_POST['email'] == "") {
         $errors[] = "The email field is empty";
      }
     if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
          $errors[] = "The email address was not valid";
      }
    if($_POST['subject'] == "") {
         $errors[] = "Please enter your subject";
      }
      if($_POST['comment'] == "") {
         $errors[] = "The comment field is empty";
      }
    if ($_REQUEST['captcha_entered']!=$_SESSION['rand_code']) { 
     $errors[] = "The math is incorrectly";
      }
      if(count($errors) == 0) {
         $sendto = "xx";//Your email goes here
         $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
     $email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
     $subject = $_POST['subject'];//You can change your subject here
     $comment = filter_var(nl2br($_POST['comment']), FILTER_SANITIZE_STRING);
      
     $message = "<strong>$name</strong>Messaggio inviato dal sito web di luigi amorfini tramite il modulo contatti:
    
    <p><strong>Name:</strong> <em>$name</em></p>
    
        <p><strong>Email:</strong> <em>$email</em></p>
        
        <p><strong>The subject:</strong> <em>$subject</em></p>
    
    <p><strong>Message:</strong> <em>$comment</em></p>";
    
    $headers = "From: $name <$email> \r\n";
    $headers .= "X-Mailer:PHP/\r\n";
    $headers .= "MIME-Version:1.0\r\n";
    $headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
 
         if(mail($sendto, $subject, nl2br($message), $headers)) {
             $success = true;
         } else {
             $success = false;
         }
    } else {
       $success = false;
 
    }
  }
 
  if(isset($_POST['submit'])) {
     if($success == true & count($errors) == 0) {
        echo "<script>alert('Thank you for your $name, Messagio inviato.');</script>";
     }
     if(count($errors) == 0 & $success == false & isset($_POST['submit'])) {
        echo "<h2>There was a problem with our form. Please email us at youremail@email.com.</h2>";
     }
 
     if($success == false & count($errors) > 0 & isset($_POST['submit'])) {
   
        foreach($errors as $show_all) {
           echo '<span style="color:#ff0000;">'.$show_all.'</span><br>';
        }
     }
 }

// Qui la parte dove voglio inserire la funzione.

	// $name = (isset($_POST["name"])) ? trim (($_POST["name"])): '';
	$name = _isset($_POST["name"]);
	// $email = (isset($_POST["email"])) ? trim (($_POST["email"])): '';
	// $subject = (isset($_POST["subject"])) ? trim (($_POST["subject"])): '';
	// $comment = (isset($_POST["comment"])) ? trim (($_POST["comment"])): '';
	

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
   <p><label for="name">Nome </label>
   <br>
   <input type="text" name="name"  id="name" value="<?php if(isset($_POST['name'])){echo htmlspecialchars($_POST['name']);}else { echo htmlspecialchars($name); }?>"  /></p>
   <br />
   <p><label for="email">Email </label>
   <br>
   <input type="text" name="email"  id="email" value="<?php if(isset($_POST['email'])){echo htmlspecialchars($_POST['email']);}else { echo htmlspecialchars($email); }?>"  /></p>
   <br />
   <p><label for="subject">Oggetto</label>
   <br><input type="text" name="subject"  id="subject" value="<?php if(isset($_POST['subject'])){echo htmlspecialchars($_POST['subject']);}else { echo htmlspecialchars($subject); }?>"  /></p>
   <br />
   <p><label for="comment">Messaggio</label>
   <br><textarea name="comment" rows="10" cols="40"><?php if(isset($_POST['comment'])){echo htmlspecialchars($_POST['comment']);}else { echo htmlspecialchars($comment); }?></textarea></p>
     <br />
<?php echo '<img src="lib/captcha.php" />'; ?>
<br /><br><input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" />
   <input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $_SESSION['rand_code']; ?>" />
	<br /><br>   
   <p><input type="submit" name="submit" value="Invia Messaggio"></p>
 </form>
 </div>
<?php
layout_footer();
?>
 

Discussioni simili