PDA

View Full Version : PHP Form Submition


CoolAsCarlito
12-07-2009, 05:41 AM
I have this php form that I'm trying to have insert into my database however I was told that since the form is loaded through ajax that it has to have the form serialize or something? Is this true and if so how is it done.

<?php
{
print "<script src=\"/jscripts/scriptaculous/prototype.js\" type=\"text/javascript\"></script>\n";
print "<script src=\"/jscripts/scriptaculous/scriptaculous.js\" type=\"text/javascript\"></script>\n";
print "<script type=\"text/javascript\" src=\"./jscripts/ajax.js\"></script>\n";
}
?>

<?php

$option = $_GET['option'];

if ($option == 0 )

{
print '<h1 class="backstage">Match Type Management</h1><br />';
print "<h2 class=backstage>Match Types :: <a href=\"#\" onclick=\"ajaxpage('backstage_libs/matchtypes.php?option=1', 'content'); return false;\">Add New</a></h2><br />";
print '<table width="100%" class="table1">';
print '<tr class="rowheading">';
print '<td>&nbsp;</td>';
print '<td>Name</td>';
print '</tr>';
$query = "SELECT * FROM efed_list_matchtypes";
$result = mysql_query ( $query );
if ($result)
{
$i = 0;
while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
{
$sClass = 'row2';
if ($i ++ & 1)
{
$sClass = 'row1';
}
printf ( "<tr class=\"%s\">", $sClass );
print "<td valign=\"top\" align=center width=35><a href=\"#\" onclick=\"ajaxpage('backstage_libs/matchtypes.php?option=2', 'content'); return false;\">Edit</a></td>";
printf ( "<td valign=\"top\">%s</td>", $row [matchtype] );
print '</tr>';
}
print '</table>';
}else
{
print '<span>There are no match types.</span><br />';
}
print '<br />';
returnmain();

} elseif ( $option == 1)
{
include('../backstageconfig.php');
include('../backstagefunctions.php');
print '<h1 class="backstage">Match Type Management</h1><br />';
print '<h2 class="backstage">Add New Match Type</h2><br />';
print '<form name="matchtype" method="post" action="backstage.php">';
print '<table width="100%" class="table2">';
print '<tr>';
print '<td width="120" class="rowheading" valign="center">Match Type:</td><td class="row3"><input type="text" name="matchtype" class="fieldtext490" id=matchtype></td>';
print '</tr>';
print '</table><br />';
print '<input type="hidden" value="submit" name="submit" id="submit" /><input type="submit" value="Save Match Type" class="button" name="submit"></form><br />';
print '<form method="POST"><input type="submit" value="Return to Match Type List" class="button200" name="return"></form><br />';
returnmain();

} elseif ( $option == 2)
{
include('../backstageconfig.php');
include('../backstagefunctions.php');
print '<h1 class="backstage">Match Type Management</h1><br />';
print '<h2 class="backstage">Edit Match Type</h2><br />';
print '<form name="editmatchtype" method="post" action="backstage.php">';
print '<table width="100%" class="table2">';
print '<tr>';
print "<td width=\"120\" class=\"rowheading\" valign=\"center\">Match Type:</td><td class=\"row3\"><input type=\"text\" name=\"matchtype\" class=\"fieldtext490\" value=\"".$row['matchtype']."\"></td>";
print '</tr>';
print '</table><br />';
print '<input type="checkbox" name="deletematchtype"><span class="table1heading">Delete Match Type?</span><br /><br />';
print '<input type="submit" value="Edit Match Type" class=button name="editmatchtype"><br /><br />';
print '<input type="button" value="Return to Match Type List" class="button200"><br /><br />';
returnmain();
}
?>

<?php

if (isset ($_POST['submit']))
{

$matchtype = $_POST['matchtype'];

$query = "INSERT INTO `efed_list_matchtypes` (`matchtype`) VALUES ('$matchtype')";

if (@mysql_query ($query))
{
print '<p>The match type has been added.</p>';
} else

{
print '<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>';
}

}
?>

CoolAsCarlito
12-07-2009, 06:36 AM
I did some more research and found this code in a book which was written very well however I want to know if I have done this correctly.

<?php
{
print "<script src=\"/jscripts/scriptaculous/prototype.js\" type=\"text/javascript\"></script>\n";
print "<script src=\"/jscripts/scriptaculous/scriptaculous.js\" type=\"text/javascript\"></script>\n";
print "<script type=\"text/javascript\" src=\"./jscripts/ajax.js\"></script>\n";
}
?>

<?php

$option = $_GET['option'];

if ($option == 0 )

{
print '<h1 class="backstage">Match Type Management</h1><br />';
print "<h2 class=backstage>Match Types :: <a href=\"#\" onclick=\"ajaxpage('backstage_libs/matchtypes.php?option=1', 'content'); return false;\">Add New</a></h2><br />";
print '<table width="100%" class="table1">';
print '<tr class="rowheading">';
print '<td>&nbsp;</td>';
print '<td>Name</td>';
print '</tr>';
$query = "SELECT * FROM efed_list_matchtypes";
$result = mysql_query ( $query );
if ($result)
{
$i = 0;
while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
{
$sClass = 'row2';
if ($i ++ & 1)
{
$sClass = 'row1';
}
printf ( "<tr class=\"%s\">", $sClass );
print "<td valign=\"top\" align=center width=35><a href=\"#\" onclick=\"ajaxpage('backstage_libs/matchtypes.php?option=2', 'content'); return false;\">Edit</a></td>";
printf ( "<td valign=\"top\">%s</td>", $row [matchtype] );
print '</tr>';
}
print '</table>';
}else
{
print '<span>There are no match types.</span><br />';
}
print '<br />';
returnmain();

} elseif ( $option == 1)
{
include('../backstageconfig.php');
include('../backstagefunctions.php');
print '<h1 class="backstage">Match Type Management</h1><br />';
print '<h2 class="backstage">Add New Match Type</h2><br />';
print '<form name="matchtype" method="post" action="backstage.php">';
print '<table width="100%" class="table2">';
print '<tr>';
print '<td width="120" class="rowheading" valign="center">Match Type:</td><td class="row3"><input type="text" name="matchtype" class="fieldtext490"></td>';
print '</tr>';
print '</table><br />';
print '<input type="submit" value="Save Match Type" class="button" name="submit" onClick="matchtypes()")></form><br />';
print '<form method="POST"><input type="submit" value="Return to Match Type List" class="button200" name="return"></form><br />';
returnmain();

} elseif ( $option == 2)
{
include('../backstageconfig.php');
include('../backstagefunctions.php');
print '<h1 class="backstage">Match Type Management</h1><br />';
print '<h2 class="backstage">Edit Match Type</h2><br />';
print '<form name="editmatchtype" method="post" action="backstage.php">';
print '<table width="100%" class="table2">';
print '<tr>';
print "<td width=\"120\" class=\"rowheading\" valign=\"center\">Match Type:</td><td class=\"row3\"><input type=\"text\" name=\"matchtype\" class=\"fieldtext490\" value=\"".$row['matchtype']."\"></td>";
print '</tr>';
print '</table><br />';
print '<input type="checkbox" name="deletematchtype"><span class="table1heading">Delete Match Type?</span><br /><br />';
print '<input type="submit" value="Edit Match Type" class=button name="editmatchtype"><br /><br />';
print '<input type="button" value="Return to Match Type List" class="button200"><br /><br />';
returnmain();
}
?>

<?php
function matchtypes()
{
// Include the database connection script:
require_once('../backstageconfig.php');

// Array for handling errors:
$errors = array();

// Validate the required data;
if (!empty($_POST['matchtype'])) {
$matchtype = mysql_real_escape_string($_POST['matchtype'], $dbc);
} else {
$errors[] = 'matchtype';
}

if (!$errors) { // If no errors, add the employee.

// Run the query:
$qieru = "INSERT INTO efed_list_matchtypes (`matchtype`) VALUES ($matchtype)";
$r = mysql_query($q, $dbc);

// Check that the query worked:
if (mysql_affected_rows($dbc) == 1) {

echo '<p><strong>The employee has been added.</strong></p>';

} else { // Query failure.
echo '<p class="error">The employee could not be added due to a system error.</p>';
}

} else { // Errors!
echo '<p>The following errors occurred:</p><ul class="error">';

// Print each error:
foreach ($errors as $e) {
echo "<li>Please enter a valid $e.</li>\n";
}

echo '</ul>';

}

// Close the database connection.
mysql_close($dbc);
}
?>