【问题标题】:call to php function on another page not working Uncaught ReferenceError: test is not defined在另一个页面上调用 php 函数不起作用 Uncaught ReferenceError: test is not defined
【发布时间】:2015-09-08 11:31:38
【问题描述】:

我试图在另一个页面中调用一个函数并得到:Uncaught ReferenceError: test is not defined

两个页面分别是index.php和functions.php

代码块:

索引.php

if($result) {
    // Make sure there are some files in there
    if($result == '') {
        echo '<h1>There are no files in the database</h1>';
    }
    else {

require './functions.php';
        // Print the top of a table
        echo '<table class="table-survey" style="margin-left: 50px; width: 1400px;">
                <th>
                <tr>
                    <td><b>CSSID</b></td>
                    <td><b>GROUP</b></td>
                    <td><b>Class</b></td>
                    <td><b>Gross Commission Amount</b></td>
                    <td><b>Name</b></td>
                    <td><b>Email Address</b></td>
                    <td><b>Email Received</b></td>
                    <td><b>Email Sent</b></td>
                    <td><b>Notes from December</b></td>
                    <td><b>Not Used For Business</td>
                </tr>
                </th>';
        // Print each file
        while ($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['cssid']}</td>
                    <td>{$row['grp']}</td>
                    <td>{$row['css_class']}</td>
                    <td>$" . number_format($row['gross_commission_amount'], 2) . "</td>
                    <td>{$row['FName']} {$row['LName']}</td>
                    <td>{$row['email_address']}</td>
                    <td>{$row['email_received']}</td>
                    <td>{$row['email_sent']}</td>
                    <td>{$row['additional_notes']}</td>";
if($delemail == $row) {
                echo "<td><form><input value={$row['email_address']} type='radio' name='selected_already' checked='checked'></input></form>/td>";
}
else{

echo "<td><form method='post' action='functions.php'><input value={$row['email_address']} type='radio' name='optradio' onchange='test(this.value);'></input></form></td>";

}
              echo "</tr>";

functions.php

function test(){

if (!$link = mysql_connect('localhost', 'dummydata', 'dummydata')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('test_table', $link)) {
    echo 'Could not select database';
    exit;
}


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

$sql = "update email_data set additional_notes_new = case when additional_notes_new is null then 'NOT USED FOR BUSINESS' else concat(additional_notes_new, 'NOT USED FOR BUSINESS') END WHERE email_address = '$delemail' and additional_notes_new NOT LIKE '%NOT USED FOR BUSINESS%'";
$result = mysql_query($sql,$link);



}
return false;

};

所有 POST 和其他数据都在工作,包括粘贴代码之前的 SQL 语句。一旦我单击单选按钮调用该函数,我就会收到错误消息。请原谅我仍在学习的代码。

【问题讨论】:

标签: php function include require


【解决方案1】:

您的test 函数是php,如果您正在执行onchange 调用,它将尝试在Javascript 中找到一个名为test 的函数。如果您想通过 onclick 事件调用 test 函数而不刷新页面,您正在寻找名为 ajax 的内容,可以在此处找到有关 ajax 的更多信息:http://www.w3schools.com/php/php_ajax_php.asp

【讨论】:

  • @JayBlanchard 抱歉,本来是onchange 函数,它在他最后一个echo 来自index.php
  • 谢谢格顿,我很确定这就是我所需要的。今天早上我要修改电话。
【解决方案2】:

你可以这样做:

onchange='test(this.value);'

  onClick='$.post("somewhere.php",{posteddata:$(this).val()},function(){ })'

它会 $_POST['posteddata'] 到某处.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2020-12-26
    • 2021-12-08
    • 2019-01-22
    • 2019-12-31
    相关资源
    最近更新 更多