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.
you can do this:
onchange='test(this.value);'
to
onClick='$.post("somewhere.php",{posteddata:$(this).val()},function(){ })'
its will $_POST['posteddata'] to somewhere.php
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