Stavros B
Stavros B

Reputation: 177

Load php function in js file and use function

I have the following php function.

public function dateIndaysoff($mydate=false){
        if(!$mydate)return false;

        $host = "localhost";
        $user = "user";
        $pass = "pass";
        $databaseName = "database";
        $tableName = "table";
        $con = mysql_connect($host,$user,$pass);
        $dbs = mysql_select_db($databaseName, $con);

       // $db=JFactory::getDbo();
        $dbs->setQuery("select date from table WHERE `date`='$mydate'")->query();
        return (int) $db->loadResult();
    }

This function searches an input value inside a database table column and if it finds then we have a TRUE, else FALSE.

So, i have a jquery inside .js file where i execute a specific action and i want to check if i have a TRUE or FALSE result. In jquery i use a variable called val. So inside jquery in some place i want to have something like this:

if (dateIndaysoff(val)) {something}

Any ideas?

Upvotes: 0

Views: 481

Answers (4)

Stavros B
Stavros B

Reputation: 177

Ok if i got this right i have to do this:

1st) Create a .php file where i will insert my php function and above the function i will put this:

$mydate = $_POST['val'];

where $mydate is the result of the function as you can see from my first post and val is the variable i want to put in $mydate from ajax.

2nd) I will go inside .js file. Now here is the problem. Here i have a code like this:

jQuery(".datepicker").change(function() {
 var val = jQuery(this).datepicker().val();
 console.log(val);
  if (dateIndaysoff(val)) {
   console.log("hide");
    jQuery('.chzn-drop li:nth-child(9)').hide();
    jQuery('.chzn-drop li:nth-child(10)').hide();
 } else {
   console.log("show");
     jQuery('.chzn-drop li:nth-child(9)').show();
     jQuery('.chzn-drop li:nth-child(10)').show();
 }
});

Inside this code, in the first if, i want to see if the variable val is inside my database table. So, how could i write correctly this jQuery with the Ajax you propose in order for this to work? Also please take a look maybe i have a mistake in my php function (in this function i want to connect with a database and take a TRUE if $myvalue is inside a table column)

Upvotes: 0

Marc Compte
Marc Compte

Reputation: 4819

You can do it with AJaX. Something like this:

A PHP file with all the functions you are using (functions.php):

function test($data) {
     // ...
}

A JS to request the data:

function getTest() {
     $.ajax('getTestByAJaX.php', {
         "data": {"param1": "test"},
         "success": function(data, status, xhr) {
         }
     });
}

getTestByAJaX.php. A PHP that gets the AJaX call and executes the PHP function.

require 'functions.php';
if (isset($_REQUEST["param1"])) {
     echo test($_REQUEST["param1"]);
}

Upvotes: 0

Velimir Tchatchevsky
Velimir Tchatchevsky

Reputation: 2825

Instead of wrapping the php code in a function you can wrap it in a if($_POST['checkDate']){//your code here}, then in javascript make an ajax request (http://www.w3schools.com/ajax/), which sends a parameter named checkDate and in the success block of the ajax call you can have your code you represented as {something}

function checkDate(){
   $.post('yourPhpFile.php', {checkDate:$dateToBeChecked}, function(data){
    if(data){alert("true")};
   });
};

and the php:

if($_POST['checkDate']){
    //your current function, $_POST['checkDate'] is the parameter sent from js
}

Upvotes: 1

Joey Abano
Joey Abano

Reputation: 61

Just to work with your current code.

In your php file lets say datasource.php

echo dateIndaysoff()

In your requesting file lets say index.php

$.ajax({
  url: "index.php",
  context: document.body
}).done(function( data ) {
  /* do whatever you want here */
});

Upvotes: 0

Related Questions