meeting-room-booking-system / mrbs-code

MRBS application code
Other
127 stars 63 forks source link

Get a php variable into edit_entry.js.php #2401

Open jberanek opened 5 years ago

jberanek commented 5 years ago

Hello, How can i get a variable from mrbs_sql.inc to edit_entry.js.php? I would like to send a js alert depending on an sql query. I would do the query in mrbs_sql.inc (or maybe in edit_entry.php) and then the alert would be triggered in the js.

I tried to create a new js variable using a formula like this: var alert = $('#oplower').val(); But I cant get the value from the php.

Thank you in advance. Gramanox

Reported by: *anonymous

Original Ticket: mrbs/support-requests/1698

jberanek commented 5 years ago

You'll need to give more details about what you are trying to do. Do you want to pop up an alert if somebody tries to make a certain type of booking, or a booking that contravenes a policy, once they select certain options on the edit_entry form? If so, then you're going to need to do it by making an Ajax request in your JavaScript. This will then be handled by your PHP code in the server which returns the result to the JavaScript in the client.

Original comment by: campbell-m

jberanek commented 5 years ago

Thank you, Campbell. I would like to pop up an alert when a text box contains the same text as another saved booking's text. I think in this case I need to get the info if there is a booking with the same text from the database with sql query in edit_entry.php or maybe in mrbs_sql.inc, but it is not clear how can I transfer a variable to the js. In this specific case I made a policy for it, but I don't want to prevent the booking, only want to trigger an alert window. I can see that js handles the policy massages, whose source is mrbs_sql.inc but can't figure out how these communicate each other. OK, so ajax request is the solution, but can you give an example for this case? Thank you in advance. Gramanox

Original comment by: *anonymous

jberanek commented 5 years ago

The JavaScript will send an Ajax POST request to the server whenever the booking text changes. The easiest way to do this is probably to write your own page on the server, eg check_text.php, which will read the POST data (which will include the booking text), query the database, and then return true or false (or maybe some details about the matching booking(s)) to the JavaScript. The Ajax callback function can then display an alert or make some other indication that there's a matching booking.

See https://api.jquery.com/jquery.post/ for more details on how to submit an Ajax POST request.

Original comment by: campbell-m

jberanek commented 5 years ago

Thank you again. I tried it for a day, but no success... I am not a professional coder, I couldnt solve the problem that way.

I am very close with an alternative way: I made a new function in the file mrbs_sql.inc , and I can call the variable in edit_entry.js.php using
var warning = "<?php echo $WAR_alert2; ?>"; But now my problem is that the sql functions dont work in the new function :(( For example sql_escape and sql_query. I really don't understand. How these works in another function (ie mrbscheckpolicy) in the same file and not work in my function? I'm confused...

Thank you

Original comment by: gramanox

jberanek commented 5 years ago

An Ajax call is the only way you are going to be able to do it. The reason being that when the page is first loaded you don't know what the user is going to type in. So you have to wait until the user types something in and then the JavaScript can make an Ajax call.

I don't know why your code isn't working, but if you are trying to use sql_escape() etc. you must be using an old version of MRBS. I'd recommend upgrading to the latest version (but then of course you'll have to re-apply any custom changes you have made).

Original comment by: campbell-m

jberanek commented 5 years ago

After a hard unsuccessful fight with a custom php file my solution is that I put an error (a special one) to the the result error array of mrbsCheckPolicy. Because I want only a warning keeping the booking valid I put another condition that assures the booking remain valid with that special "error" containing two asterixes like "** This is not a real error"

if ((count($errors) > 0) && (array_search('**', $errors) == FALSE))
        {
          $valid_booking = FALSE;

        }

In the js I managed to call an alert when this "error" occours. My only problem now is that this alert comes each time the js checks the booking. I tried to apply a variable "alerted" that was set to 1 after an alert, but it doesnt work :(

Is it a solution? Thank you

//only the special error occurs:
if ((titleText.includes("**")) && (result.rules_broken.length === 1))
             {
             policyDiv.text(checkMark).attr('class', 'good');

             }

//alert in the case of special error
             if (titleText.includes("**"))
             {                         
             if (alerted != 1)
             {
             alert("You are alerted!");
             alerted=1;
             }

Original comment by: gramanox

jberanek commented 5 years ago

I still think that the custom php file with an Ajax call is the way to go . For an example see del_entry_ajax.php.

Original comment by: campbell-m

jberanek commented 5 years ago

OK,thanks, now I'm trying to create again a new php and js function for it. My first problem is that I get get back a html code if I insert this to the edit_entry.js.php.

This is only for understand the idea. So the js should send the "param" to the php and the php should return the result6 variable in this case, but I get a html code similar of the current page (edit_entry.php)...

$('#f_field').change (function() 
  {
  var param = 28;
  $.post('check_mutet.php', param, function(result6) {

          alert(result6);

    });

   }); 

while check_mutet.php contains this

<?php
// $Id$

require "defaultincludes.inc";
require_once "mrbs_sql.inc";

// Check the user is authorised for this page
checkAuthorised();

$result6=666;

echo $result6;

Thank you for your help :)

Original comment by: gramanox

jberanek commented 5 years ago

That looks correct, so I don't understand why you are not getting 666 popped up in an alert.

You can always see what is being sent and received by looking at Developer Tools in your browser. If you are using Chrome then it's under the Network menu item. Click on check_mutet.php and you should see the parameters sent under Headers and the result under Response.

Original comment by: campbell-m

jberanek commented 5 years ago

OK, nice, thank you, I comented the checkauthorised (it is not neccessary in this case) and it works! Now I get back the value, but there is another question: whgat parameters should I send as the booking data to the php code?

What should be in the place of "28" to send the data (params) of the booking to the php?

Thanx

My php code is this one:

function TAJ(&$booking)
{

  global $tbl_entry;

  $mutetkezd=$booking['start_time'];
  $tajszam=$booking['TAJ'];

  //TAj could be 123456789 or 123-456-789
  if(strpos($tajszam, "-") === false)

{
  $tajszam2=substr_replace($tajszam, '-', 3, 0);
  $tajszam2=substr_replace($tajszam2, '-', 7, 0);
}
else

{
      $tajszam2= preg_replace('/[^\w]/', '', $tajszam);
}

  $sql_TAJ = "SELECT *
          FROM $tbl_entry 
        WHERE (type = 'E' OR type='I')
                        AND (TAJ = '" . sql_escape($tajszam) . "' OR TAJ = '" . sql_escape($tajszam2) . "')
            AND start_time > $mutetkezd
                        AND (room_id = 1 OR room_id = 2 OR room_id = 3 OR room_id = 5)";

  $TAJsz = sql_query($sql_TAJ);
 $TAJsz2 = sql_count($TAJsz); 

 if ($TAJsz2 > 0)
  {
      $TAJ_alert=1;
   }
  else 
  {
     $TAJ_alert=0;

  }

  return $TAJ_alert;
}

$result6=TAJ($booking);

echo $result6;

And my js code:

$('#f_field').change (function() 
  {
  var param = 28;
  $.post('check_mutet.php', param, function(result6) {

          alert(result6);

    });

   }); 

Original comment by: gramanox

jberanek commented 5 years ago

I haven't quite studied your PHP code in detail, but from what you were saying earlier you are trying to put up an alert if the text in a field is the same as the text in an existing booking. In which case the JavaScript presumably needs to pass the text in the field, which you can with $(this).val(), eg

$('#f_field').change (function() 
  {
  var param = $(this).val();
  $.post('check_mutet.php', param, function(result6) {

          alert(result6);

    });

   });

Your PHP code will then read this parameter using the MRBS function get_form_var() and then use it in the SQL query.

Original comment by: campbell-m

jberanek commented 5 years ago

By the way, if you are using sql_escape() etc. then it must mean that you are using an old version of MRBS. I'd recommend upgrading to the latest version sometime - maybe after you've got this working.

Original comment by: campbell-m

jberanek commented 5 years ago

OK thank you now finally it WORKS :) Unfortunately I made many-many changes in the original mrbs files so I wouldn't like to upgrade, it would be a suicide now... Maybe some time it should be recoded by a professional coder using the new version.

Original comment by: gramanox

jberanek commented 5 years ago

I comented the checkauthorised (it is not neccessary in this case) and it works!

It's probably a good idea to leave the checkAuthorised() in and make it work by assigning check_mutet.php a page level (the same as edit_entry.php) in mrbs_auth.inc.

Original comment by: campbell-m