如何在PHP中插入onsubmit

时间:2011-08-30 15:56:12

标签: php javascript ajax

我需要将一些表单值插入到db表中,一旦用户单击按钮并运行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>

3 个答案:

答案 0 :(得分:3)

PHP在服务器端执行。如果要在不重新加载页面的情况下调用该函数,则必须使用AJAX调用,例如使用jQuery。

你可以通过谷歌找到数十亿的教程。

答案 1 :(得分:0)

<?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>

答案 2 :(得分:0)

PHP无法以这种方式工作。如果您希望在表单提交时激活PHP脚本,则需要将表单提交到PHP页面,并在该页面上 把你的脚本,例如

<form action=submit.php method=post>

    <input type=text name=text>
    <input type=submit>

</form>

接下来,在 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
    }

?>

如果您希望它在没有页面重新加载的情况下工作,您将不得不使用一些客户端技术。最受欢迎的是 AJAX