I need to insert some form values into a db table , how would I create a function to call once the user clicks on the button and run the insert scrtipt .
<form name="quiz_info" method="post">
<?php
echo $this->quiz->title;
echo $mainframe->getPageTitle();
echo '<p><input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">' . JText::_('I have Read and Acknowledge the procedure'). '</label></p>' ;
echo '<input id="proceedButton" name="proceedButton" disabled="true" value="' . JText::_('Acknowledge') . '" type="submit" />' ;
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->开发者_高级运维get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge` (course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
?>
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP is executed on the server-side. If you want to call the function without reloading the page you will have to use an AJAX call, for example with jQuery.
You can find billions of tutorials through google.
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge`(course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $this->quiz->title; ?>
<?php echo $mainframe->getPageTitle(); ?>
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed"><?php echo JText::_('I have Read and Acknowledge the procedure'); ?></label>
<input id="proceedButton" name="proceedButton" disabled="true" value="<?php JText::_('Acknowledge'); ?>" type="submit" />
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP doesn't work this way. If you wish to activate your PHP script on form submit, you'll need to submit the form to a PHP page, and on that page to put your script, for example
<form action=submit.php method=post>
<input type=text name=text>
<input type=submit>
</form>
Next, on submit.php:
<?php
if (!empty($_POST['text'])) { //If the POST variable set by input named 'text' is not empty...
echo $_POST['text']; //Print it on the screen.
} else { //If it is empty
echo "No form submission detected"; //Print an error
}
?>
If you want it to work without a page reload, you'll have to use some client side technology. The most popular one for that purpose is AJAX
精彩评论