Extension To Use HTML Form And PHP To Insert Data In MySQL
Posted: Fri Apr 08, 2011 4:06 am
I am looking for an extension for Joomla 1.6 that will allow me to create an HTML form in a Joomla article that when submitted will use custom PHP code to save the form data into a MySQL database on the server. Basically the user will fill in the HTML form in the Joomla article and when they hit the submit button the form will process the user's data via the custom PHP code and save the user's data into a MySQL database that is setup to store that data from the form.
I have the working HTML form and custom PHP code working already outside of Joomla but when I attempt to add the form to an Joomla article using DirectPHP to run the PHP in the article the form submit action does not work. I have tried using numerous option for the FORM ACTION value including the current URL of the Joomla article that the form is on however when you hit the submit button the page just refreshes, erases the form and does not submit the data into the MySQL database. Does anyone have any advise on how to submit the form in a Joomla article and have the PHP run on the same page?
Here is the code I am trying to use:
<?php
echo "<H2>Add a Car</H2><HR>";
function handleform()
{
global $UserName, $UserCarMake, $UserCarModel, $UserCarYear, $UserCarColor, $UserCarPlate;
$dbh=mysql_connect ("localhost", "username", "password");
if (!dbh)
{
die("Failed to open the Database");
}
mysql_select_db("joomladatabase");
if(mysql_errno())
{
die("<BR>" . mysql_errno().": ".mysql_error()."<BR>");
}
$query = "INSERT INTO RegisteredUserCars (UserName, UserCarMake, UserCarModel, UserCarYear, UserCarColor, UserCarPlate)
VALUES ('$UserName', '$UserCarMake', '$UserCarModel', '$UserCarYear', '$UserCarColor', '$UserCarPlate')";
$result = mysql_query($query);
if (mysql_errno())
{
die("<BR>" . mysql_errno().": ".mysql_error()."<BR>");
}
else
{
echo "Database has been updated.";
}
}
if ($beensubmitted)
{
handleform();
}
?>
<FORM METHOD="POST" ACTION="AddRegisteredCars.php">
<P><B>Enter Your Car Make</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarMake"></P>
<P><B>Enter Your Car Model</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarModel"></P>
<P><B>Enter Your Car Year</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarYear"></P>
<P><B>Enter Your Car Color</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarColor"></P>
<P><B>Enter Your Car's License Plate Number</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarPlate"></P>
<BR><INPUT TYPE="HIDDEN" NAME="beensubmitted" VALUE="TRUE">
<P><INPUT TYPE="SUBMIT" NAME="submit" VALUE="SUBMIT"></P>
</FORM>
I have the working HTML form and custom PHP code working already outside of Joomla but when I attempt to add the form to an Joomla article using DirectPHP to run the PHP in the article the form submit action does not work. I have tried using numerous option for the FORM ACTION value including the current URL of the Joomla article that the form is on however when you hit the submit button the page just refreshes, erases the form and does not submit the data into the MySQL database. Does anyone have any advise on how to submit the form in a Joomla article and have the PHP run on the same page?
Here is the code I am trying to use:
<?php
echo "<H2>Add a Car</H2><HR>";
function handleform()
{
global $UserName, $UserCarMake, $UserCarModel, $UserCarYear, $UserCarColor, $UserCarPlate;
$dbh=mysql_connect ("localhost", "username", "password");
if (!dbh)
{
die("Failed to open the Database");
}
mysql_select_db("joomladatabase");
if(mysql_errno())
{
die("<BR>" . mysql_errno().": ".mysql_error()."<BR>");
}
$query = "INSERT INTO RegisteredUserCars (UserName, UserCarMake, UserCarModel, UserCarYear, UserCarColor, UserCarPlate)
VALUES ('$UserName', '$UserCarMake', '$UserCarModel', '$UserCarYear', '$UserCarColor', '$UserCarPlate')";
$result = mysql_query($query);
if (mysql_errno())
{
die("<BR>" . mysql_errno().": ".mysql_error()."<BR>");
}
else
{
echo "Database has been updated.";
}
}
if ($beensubmitted)
{
handleform();
}
?>
<FORM METHOD="POST" ACTION="AddRegisteredCars.php">
<P><B>Enter Your Car Make</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarMake"></P>
<P><B>Enter Your Car Model</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarModel"></P>
<P><B>Enter Your Car Year</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarYear"></P>
<P><B>Enter Your Car Color</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarColor"></P>
<P><B>Enter Your Car's License Plate Number</B>
<BR><INPUT TYPE="TEXT" NAME="UserCarPlate"></P>
<BR><INPUT TYPE="HIDDEN" NAME="beensubmitted" VALUE="TRUE">
<P><INPUT TYPE="SUBMIT" NAME="submit" VALUE="SUBMIT"></P>
</FORM>