Ok so ive looked at AJAX and to be honest im having trouble understanding how i am going to apply it to my code. Il go into more depth about what i am trying to do, and maybe someone can help me better understand how to incorporate an AJAX call. So i have one function, called add_signups which takes in 3 parameters and inserts some data into a mysql table.
function add_signup($society, $student_email, $event) {
$member_fields = array('name','student_num','student_email',);
$fields = '`' . implode('`, `',($member_fields)) . '`';
$query = mysql_query("SELECT $fields FROM `$society members` WHERE `student_email`='$student_email'");
$elems = mysql_fetch_assoc($query);
array_walk($elems, 'array_sanitize');
$data = '\'' . implode('\', \'', $elems) . '\'';
$result = mysql_query("INSERT INTO `$event` ($fields) VALUES ($data)");
}
I have another method, called get_names which prints a list of email addresses. I am trying to call the add_signup method once a user clicks on one of the email addresses, this will insert that users email address, along with some other info into the table.
function get_names($society, $event){
$records= mysql_query("SELECT `student_email` FROM `$society members` ORDER BY `student_email`");
while($emails = mysql_fetch_assoc($records)){
echo ' <li style="cursor:pointer;" onclick="' add_signup($society, $emails['student_email'], $event) '); ">' .$emails['student_email'] . "</li>";
}
}
What i dont understand is where to put what in terms of using an ajax call. Im thinking i need to put the add_signup() method in a seperate php file, then in the html file within <script>
tags put the following
function jsfunction(){
$.ajax({
type: "POST",
url: "add_signupmethod.php",
data: { "what to put here??" },
success: " what to put here? "
});
}
obviously i would need to change the onclick event to call the js function but is that anywhere near correct?
Or do i even need to use ajax and is there a much easier way of doing it? Ive been stuck on this for almost two days now, the frustrating thing is i know that the add_signup()
function works as i tested it outside of the otherfunction. Would greatly appreciate anyone helping me out with this.
Original message
echo ' <li style="cursor:pointer;" onclick="' add_signup($society, $emails['student_email'], $event) '); ">' .$emails['student_email'] . "</li>";
Can anyone help with this? Im trying to call a php function once a list element is clicked, only the html code is being generated itself by another php function in an echo statement.
I keep getting syntax errors so is it as simple as that or is it even possible at all to do this? if not, what could i do to get the result i want? the actual error code is
Parse error: syntax error, unexpected 'add_signup' (T_STRING), expecting ',' or ';' in C:\Users. . .