-1

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. . .
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
mcgovec9
  • 71
  • 7
  • Just to clarify, you aren't expecting the php function to be run when the user clicks correct? – cwurtz Mar 13 '15 at 16:54
  • possible duplicate of [Call php function from javascript](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript) – l-x Mar 13 '15 at 17:01
  • i want the php code to run once the user clicks on the list element. im pretty new to php so forgive me if i am going about it in the entirely wrong way :p – mcgovec9 Mar 13 '15 at 17:29

1 Answers1

2

You're missing the dot (concatenation) before and after your add_signup method.

echo ' <li onclick="document.write(' . add_signup($society, $names['name'], $event) . '); ">' . $names['name'] . "</li>"; 

That will get rid of your syntax error. However it will also execute the add_signup method immediately, and not when the button is clicked.

To call this when the button is clicked, start looking into how to make Ajax calls client-side.

JoeCoder
  • 870
  • 5
  • 4