Saturday, 27 December 2014

Tagged under:

Processing the Form Send Data (Php Code)

we are going to create our PHP file that will process the data.

When you submit your HTML form PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or POST data, respectively.

Therefore, a form input called 'Name' that was sent via POST, would be stored as $_POST['Name'].



Copy and paste this code and save it as send.php

<?php
//Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)

   $_POST['Comments'] = nl2br($_POST['Comments']);
   //Check whether a $_GET['Languages'] is set
   if ( isset($_POST['Colors']) ) {
     $_POST['Colors'] = implode(', ', $_POST['Colors']); //Converts an array into a single string
   }

   //Let's now print out the received values in the browser
   echo "Your name: {$_POST['Name']}<br />";
   echo "Your password: {$_POST['Password']}<br />";
   echo "Your favourite season: {$_POST['Seasons']}<br /><br />";
   echo "Your comments:<br />{$_POST['Comments']}<br /><br />";
   echo "You are from: {$_POST['Country']}<br />";
   echo "Colors you chose: {$_POST['Colors']}<br />";
} else {
    echo "You can't see this page without submitting the form.";
}
?>

GET and POST
When defining the technique to send information to the PHP script, you either use GET or POST. Both send variables across to a script, however they achieve this in various ways.

The GET method has a control on the amount of information than could be sent. Consequently, if you send long variables using GET, you will likely lose large amounts of them.

The POST method sends its variables behind the scenes and has no limits on the amount of information to be sent. Because the variables aren't displayed in the URL, it is extremely hard to bookmark the page.
Use of  This Examples :
See below for what I roughly use... some things are changed though, like I've a good little goto() function, in place of writing out the address twice in a header/exit call.

Copy and paste this code :
##################################
<?php
//--------------------------------------------------
// Return the required values for this script

$act = isset($_POST['act'] ? $_POST['act'] : '');
$name = isset($_POST['name'] ? $_POST['name'] : '');

//--------------------------------------------------
// By default there are no errors

$htmlErrors = array();

//--------------------------------------------------
// If the user has submitted the form

if ($act != '') {

//--------------------------------------------------
// Validation

if (trim($name) == '') {
$htmlErrors['name'] = 'Your name is required';
}

if (strlen($name) > 100) {
$htmlErrors['name'] = 'Your name cannot be longer than 100 characters';
}

//--------------------------------------------------
// If the data is valid

if (count($htmlErrors) == 0) {

//--------------------------------------------------
// Do something...

//--------------------------------------------------
// Send the user onto the thank-you page

header('Location: ./thankyou/');
exit('<p>Goto <a href="./thankyou/">next page</a></p>');

}
}
?>

<form action="./" method="post" class="basicForm">
<fieldset>
<legend>Form</legend>

<?php
if (count($htmlErrors) > 0) {
echo '<ul class="error">';
foreach ($htmlErrors as $err) echo '<li>' . $err . '</li>';
echo '</ul>';
}
?>

<div class="row<?= (isset($htmlErrors['name']) ? ' error' : '') ?>">
<span class="label"><label for="fldName">Name:</label></span>
<span class="input"><input type="text" name="name" id="fldName" value="<?= htmlentities($name) ?>" /></span>
</div>

<div class="row submit">
<input type="hidden" name="act" value="submit" />
<input type="submit" value="Send" />
</div>

</fieldset>
</form>

Passing Dynamically Selected Value from a database through an HTML Form in PHP 5?
HTML form and use PHP code to create decisions based off this information to create dynamic webpages
<?$rb = $_GET['rb']; // value of rb got from previous form
connectToDB();
$sql="select * from category where cat='$rb' and visible='TRUE' order by cat_name";
$res=mysql_query($sql);
$submit = $_POST['submit'];
?>

<form method = "POST" value = "prod2.php">
<table cellSpacing="0" cellPadding="3" width="70%" align="center" border="1" bordercolor="#cccccc" id="table5">
<tr bgColor="#cccccc">
<td align="middle" colspan="2">
<font face="Verdana, Tahoma, Arial" size="2"><b>
<font color="#cc0033">Choose Category </font></b></font>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<?php
while($arow=mysql_fetch_array($res))
{
?>
<tr>
<td><b><font color="#cc0000" face="Verdana, Tahoma, Arial" size="2">&nbsp;<?php print $arow[cat_name]; ?></font></b></td>
<td><font face="Arial" size="2">
<input type="radio" value="<?php print $arow[cat_id]; ?>" name="sel_catid[]" checked><? print $arow[cat_id];?></font></td>
</tr>
<?
} // cat name
?>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="submit" value="Submit" style="font-family: Verdana, Tahoma, Arial; font-size: 8pt">
</td>
</tr>
</table>
</form>

0 comments:

Post a Comment