Forums

Onsubmit post data to new table

spletcher 18 May, 2009
I'm finishing up a form that hopefully will be useful to others.

I'm trying to post data to another table and am having some small snags.

In the Onsubmit box, I am testing the following code:


<p>Thank you {first_name} {last_name} for registering.</p>

<?php
$class = JRequest::getVar('classification', '', 'post');
echo $class;
$db =& JFactory::getDBO();
$query = '
INSERT INTO `#__volunteer_test` (VolunteerID, classification, title)
VALUES("1234", $class, "MD")';
$db->setQuery($query);
$db->query();

The defined variable $class shows up with the value from the form on screen, but the table won't post.

If I put the $class2 in quotes such as: INSERT INTO `#__volunteer_test` (VolunteerID, classification, title)
VALUES("1234", "$class", "MD")';
then the table posts, however the value ends up being simply $class rather than what the user filled out on the form. Do I have a syntax error here?
spletcher 18 May, 2009
Well - I may have found a fix on this.

Turns out I don't need to "fetch" the form data after all.

I changed to this and I'm fine:

<?php
$db =& JFactory::getDBO();
$query = "
INSERT INTO `#__volunteer_test` (VolunteerID, classification, title)
VALUES('1234', '$_POST[classification]', 'MD')";
$db->setQuery($query);
$db->query();
?>

Please note that I did have to change my single quote to doubles and vice versa for the syntax to work.
spletcher 18 May, 2009
Turns out my problem with the original code was also in the syntax of using the double quote and single quotes.

Here is code that works also:

<?php
$class = JRequest::getVar('classification', '', 'post');
$db =& JFactory::getDBO();
$query = "
INSERT INTO `#__volunteer_test` (VolunteerID, classification, title)
VALUES('1234', '$class', 'MD')";
$db->setQuery($query);
$db->query();
?>
This topic is locked and no more replies can be posted.