PHP form soru

4 Cevap php

Nasıl bir formdan gönderilen değerleri yakalamak ve PHP kullanarak form alanlarına sayfada onları geri görüntülenebilir mi? Zaten aşağıdaki gibidir form oluşturduk:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Order Form</title>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body>

<form name="orderform" method="get" action="ProcessOrder.php">
<p>Name<br/>
<input type="text" name="name" size="50" /><br/>
<p>Address<br/>
<input type="text" name="address" size="50" /><br/>
<p>City<br/>
<input type="text" name="city" size="50" /><br/>
<p>Province<br/>
<input type="text" name="province" size="2" /><br/>
<p>Postal Code<br/>
<input type="text" name="postalcode" size="6" /><br/>
<p>Email<br/>
<input type="reset"/>
<input type="submit"/><p>
</form>
</body>
</html>

Düzenleme: Ben bu işe değerlerini almak ve her alan için onları görüntülemek için bir php dosyası kullanmak isteseydim? Örneğin:

<?php 
    <input type="text" name="name" size="50" value="isset($_GET["name"]) ? htmlspecialchars($_GET["name"]) : ""; ?>" /><br/>
?>

4 Cevap

Her bir alan için, böyle bir şey yapın:

<input type="text" name="name" size="50" value="<?php print isset($_GET["name"]) ? htmlspecialchars($_GET["name"]) : ""; ?>" /><br/>

Ben genellikle bir çerçeve olmadan, basit formlar / siteler için bu gibi bir şey kullanabilirsiniz.

$name       = isset($_GET['name'])       ? trim($_GET['name'])       : "";
$address    = isset($_GET['address'])    ? trim($_GET['address'])    : "";
$city       = isset($_GET['city'])       ? trim($_GET['city'])       : "";
$province   = isset($_GET['province'])   ? trim($_GET['province'])   : "";
$postalcode = isset($_GET['postalcode']) ? trim($_GET['postalcode']) : "";

...

<p>Name<br/>
<input type="text" name="name" size="50" value="<?= htmlspecialchars($name); ?>" /><br/>
<p>Address<br/>
<input type="text" name="address" size="50" value="<?= htmlspecialchars($address); ?>" /><br/>
<p>City<br/>
<input type="text" name="city" size="50" value="<?= htmlspecialchars($city); ?>" /><br/>
<p>Province<br/>
<input type="text" name="province" size="2" value="<?= htmlspecialchars($province); ?>" /><br/>
<p>Postal Code<br/>
<input type="text" name="postalcode" size="6" value="<?= htmlspecialchars($postalcode); ?>" />

Bu gerekirse varsayılan değerleri ayarlamak için izin verir, ya da sadece boş bırakabilirsiniz.

Ya * aşağıdakileri yapabilirsiniz

$fields = array(
    'name'       => '', 
    'address'    => '', 
    'city'       => '', 
    'provice'    => 'Quebec', 
    'postalcode' => ''
);

foreach ( $fields as $field => $default ) {
    $$field = isset($_GET[$field]) ? trim($_GET[$field]) : $default;
}

Ve HTML aynı kalır.

* Hayır, ben bu test etmedi.

PHP manual PHP formları ile çalışmak için nasıl büyük bir öğretici vardır

<form action="action.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>


Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

Bu betiğin çıktısı olabilir:

Joe Merhaba. Siz 22 yaşındasınız.

Revize:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Order Form</title>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body>

<form name="orderform" method="get" action="ProcessOrder.php">
<p>Name<br/>
<input type="text" name="name" size="50" value="<?php echo(htmlspecialchars ($_GET['name'])); ?>/><br/>
<p>Address<br/>
<input type="text" name="address" size="50"value="<?php echo(htmlspecialchars ($_GET['address'])); ?> /><br/>
<p>City<br/>
<input type="text" name="city" size="50" value="<?php echo(htmlspecialchars ($_GET['city'])); ?>/><br/>
<p>Province<br/>
<input type="text" name="province" size="2" value="<?php echo(htmlspecialchars ($_GET['province'])); ?>/><br/>
<p>Postal Code<br/>
<input type="text" name="postalcode" size="6" value="<?php echo(htmlspecialchars ($_GET['postalcode'])); ?>/><br/>
<p>Email<br/>
<input type="reset"/>
<input type="submit"/><p>
</form>
</body>
</html>

EDIT: Üzgünüm YAYINLAYAMIYOR GET kullanılarak fark etti.