0

I am trying to call a function in another page and getting : Uncaught ReferenceError: test is not defined

Two pages are index.php and functions.php

Code Blocks:

Index.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;

};

All of the POST and other data is working including the SQL statements before the pasted code. As soon as I click the radio button to call the function I get the error. Please excuse the code I am still learning.

  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 22 '15 at 21:17
  • Are you trying to include function.php in index.php? – Jay Blanchard Jun 22 '15 at 21:20
  • Thank you Jay. I'll switch it out – James Buchert Jun 23 '15 at 11:56
  • I am trying to include functions.php so I can call the function test in it. – James Buchert Jun 23 '15 at 11:57
  • I don't see an `include()` directive anywhere in this code. – Jay Blanchard Jun 23 '15 at 12:01

2 Answers2

1

you can do this:

onchange='test(this.value);'

to

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

its will $_POST['posteddata'] to somewhere.php

0

Your test function is php, if you're doing an onchange call it will try and find a function called test in Javascript. If you want to call the test function trough an onclick event without refreshing the page you're looking for something called ajax more info on ajax can be found here : http://www.w3schools.com/php/php_ajax_php.asp

Gerton
  • 676
  • 3
  • 14