problema nella implementare una funzione con ritorno

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
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.
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
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"
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
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 [email protected].</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
Autore Titolo Forum Risposte Data
loois Ho un problema nella creazione di un sistema di commenti in PHP PHP 11
S [Javascript] [HTML] problema nella stampa degli elementi della pagina Javascript 3
P doppio problema nella memorizzazione dati PHP 11
M Ipn paypal problema nella risposta PHP 1
C Problema apertura slide nella hompage HTML e CSS 0
J Problema con link nella barra di navigazione HTML e CSS 1
S Problema con 2 script nella stessa pagina Javascript 1
P problema nella stampa di table HTML e CSS 1
S Problema nella pagina di registrazione e login PHP 2
A problema nella visualizzazione file caricato tramite ftp WordPress 0
Emix Problema nel concatenare jquery ed ajax nella stessa pagina Javascript 15
W Problema nella pubblicazione programmata di un articolo WordPress 1
D Problema nella validazione di un form con JQUERY ed AjAX jQuery 4
D Problema nella chiamata ad una funzione javascript da href Ajax 2
G Problema nella pagine dei risultati ricerca Wordpress WordPress 2
G Problema nella creazione di un'area protetta PHP 10
G problema caricamento 2 js nella stessa pagina Javascript 0
max_400 Problema apostrofo nella ricezione della variabile per poi scriverla dentro un file PHP 6
giancadeejay Intro flash,problema nella visualizzazione su sito Flash 2
F problema sintassy nella mia query MySQL 6
A Problema nella creazione di un area protetta!!!! Classic ASP 3
F problema nella connessione con mysql presente in Aruba PHP 3
Dragon Problema di caricamento swf e img nella stessa pagina Flash 8
L Problema nella visualizzazione HTML e CSS 0
K problema nella formattazione del valore di una variabile PHP 0
I Sto progettando nuovi siti utilizzando bootstrap e devo dire funziona bene, l'unico problema e la maschera -moz- HTML e CSS 0
K Problema form update PHP 2
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
S Problema nel ciclare un json Javascript 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
K Problema Inner join PHP 1
F firefox problema http Linux e Software 0
N Problema con position absolute e overflow HTML e CSS 4
E Problema jquery Success jQuery 2
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
E problema selezione sfumata Photoshop 2
K [PHP] Problema con variabili concatenate. PHP 1
A Problema filtro fluidifica Photoshop Photoshop 1
H Problema Bordi Scontorno Photoshop 1
O problema con query PHP 4
R Problema installazione Realtek WiFi USB rtl8821 Reti LAN e Wireless 0
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
Y Problema percorso file in rete PHP 1
N Problema SEO "L'URL non si trova su Google" SEO e Posizionamento 4
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
P Problema acquisizione clienti Webdesign e Grafica 1

Discussioni simili