-2

I am a newbie in PHP.

I need to run this jQuery code to PHP.

Class active just change display:none to display:block

The code is here http://jsbin.com/weribi/1/

$(document).ready(function(){

  $(".cecutient-btn").click(function(){
    event.preventDefault();
    $(this).parent().find(".more-info-open")
    .slideToggle("slow")
    .toggleClass("active");

  });

});

What do I need to do?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • You need to use ajax – vaso123 Dec 19 '14 at 10:42
  • 1
    Well you cannot execute php code on click of button from the client side because jquery is client side and php is server side. You need to learn and try with ajax..http://api.jquery.com/jquery.ajax/ – 웃웃웃웃웃 Dec 19 '14 at 10:43
  • You cannot run php from a button. You can make an Ajax call to a php page to make it run – Marco Mura Dec 19 '14 at 10:43
  • lookup jquery ajax on the net. That should help you. Check it out here [http://api.jquery.com/jquery.ajax/] – Aditya Dec 19 '14 at 10:43
  • PHP is interpreted on **your server** while `click` event is triggered on user **browser**. You should use Ajax. – Hieu Le Dec 19 '14 at 10:43

2 Answers2

2

Do an ajax Call on the click event

Study , and learn ! Start from here

$(document).ready(function(){

  $(".cecutient-btn").click(function(){
    event.preventDefault();
    $(this).parent().find(".more-info-open")
    .slideToggle("slow")
    .toggleClass("active");

    $.ajax({
      type: "GET",
      url: "yourFile.php", //your file .php
      data: data,
      success: function(data) {

      }
    });

  });

});

An ajax call without jquery [info here]

function loadXMLDoc() {
    var xmlhttp;

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 ) {
           if(xmlhttp.status == 200){
               document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
           }
           else if(xmlhttp.status == 400) {
              alert('There was an error 400')
           }
           else {
               alert('something else other than 200 was returned')
           }
        }
    }

    xmlhttp.open("GET", "ajax_info.txt", true);
    xmlhttp.send();
}
Community
  • 1
  • 1
WhiteLine
  • 1,919
  • 2
  • 25
  • 53
0

If you want to run php code from a click event you'd need to do an ajax call to your php page. php is a serverside language and can't be run on a browser.

check out the docs for jquery's ajax functions here

you'd want something like

$.ajax({
    url: "url/to/myPhpPage.php"
}).done(function() {
    //callback code here
});
atmd
  • 7,430
  • 2
  • 33
  • 64