This topic is locked
[SOLVED]

  JavaScript onLoad Event

9/4/2011 8:29:11 PM
PHPRunner General questions
O
orodriguez author

Hello,
I'm using a php button to run a mysql update query. This button affected all the records. So i decided to add a custom button for each record so i can do some extra stuff using jquery in the JavaScript onLoad page.
But... what would be the sintax to run a sql query from there ???
If used, in the php button:

$sql = "update b_cabin_agenda set estado='DISPONIBLE' where id_cabage=" .$val["id_cabage"]." and estado=''";

CustomQuery($sql);
I thought was right to do:

$sql = "update b_cabin_agenda set estado='DISPONIBLE' where id_cabage=" + $(this).attr('id') +" and estado=''";

CustomQuery($sql);
Also tried:

$strSQL = "update b_cabin_agenda set estado='DISPONIBLE' where id_cabage="+ mide +" and estado=''";

$rs = CustomQuery($strSQL);

$record = db_fetch_array($rs);
Please, what should be the right sintax ???
Where in the PHPRunner help file would i read about this ???
THANKS!

admin 9/5/2011

You need to pass field values to PHP code. Buttons won't work for this purpose as button code will be the same for all records while you need to have access to the current record data.
I recommend to check this blog post first:

http://xlinesoft.com/blog/2011/04/28/taming-the-beast-events-buttons-and-code-snippets/
Here is how you can make it work:

  1. Create a separate PHP file where your PHP code for this task will be placed. Lets assume file name is doit.php
  2. Set 'View as' type of one of field to 'Custom' and pass record ID via URL. Example:

$value = "<a href='doit.php?id=".$data["id"]."'>Do something</a>";


You can make this link look like a button if you need to.
3. In doit.php file you can access the value of ID field as $_REQUEST["id"]

O
orodriguez author 9/5/2011

Thanks for your reply, i'll do what suggested.
But the way, i already have added an alias field in the sql query what is only shown in the list page.

In the html editor, used that alias with a custom view adding this code

$value = "<input type=\"button\" name=\"activar\" value=\"Activar disponible\" id=\"".$data['id_cabage']."\" />";


where id_cabage is my table id field.
I'll let you know how works what you told me. Thanks

O
orodriguez author 9/5/2011



You need to pass field values to PHP code. Buttons won't work for this purpose as button code will be the same for all records while you need to have access to the current record data.
I recommend to check this blog post first:

http://xlinesoft.com/blog/2011/04/28/taming-the-beast-events-buttons-and-code-snippets/
Here is how you can make it work:

  1. Create a separate PHP file where your PHP code for this task will be placed. Lets assume file name is doit.php
  2. Set 'View as' type of one of field to 'Custom' and pass record ID via URL. Example:

$value = "<a href='doit.php?id=".$data["id"]."'>Do something</a>";


You can make this link look like a button if you need to.
3. In doit.php file you can access the value of ID field as $_REQUEST["id"]


Sergey,
Doing what suggested, about using doit.php file to store my php code:
1.- Where do i have to store the file so it will be available for my project and NOT having to upload it by my self through ftp to the site ??

2.- Let's say i have 2 or more functions in that file, how do i access the specific funtion in that file ??

For instance, i have function REVISAR y LEER, how should i modify following code to access REVISAR function in the doit.php file ??

$value = "<a href='doit.php?id=".$data["id"]."'>Do something</a>";


That way i won't need to be adding TOO meny files.
Thanks

O
orodriguez author 9/7/2011

I've solved this issue and i'd like to share what i've done, so it could be helpfull to anyone else.
Executing javascript for a button in an specific row on the list page.
We need to identify the button as unique so i add an alias in the sql query editor adding this:



SELECT

b_cabin_agenda.id_cabage,

b_cabin_agenda.estado,

0 AS ac_estado,

FROM b_cabin_agenda


Note that id_cabage is the index in that table
Then in the fields page, only select ac_estado field to be shown in the list page.
In the visual editor, in the custom view of the field ac_estado, i add this code

$value = "<input type=\"button\" name=\"activar\" value=\"Activar disponible\" id=\"".$data['id_cabage']."\" />";


That way, each button will have an unique id taken from the id_cabage field.
I also, in same page, add an id for other field, in this case estado, to make some modify actions after click be done



if ($value =="DISPONIBLE") {

$color="black";

} else {

$color="red";

}
$value= "<span style='color:".$color."' id=\"est".$data['id_cabage']."\"> ".$value." </span>";


Then in the event page, i'll add in the list page, on the JavaScript OnLoad evet what follows:



$("input[type='button'][name='activar']").click(function(e){

//in next line i save the id of the button in var named mide

var mide = $(this).attr('id');

//i make a call using jquery ajax to an external file added to projectfolder/source folder, named funciones2m.php

$.ajax({

type: "POST", //we use POST method, important for the external php file

url: "funciones2m.php", //php file is indicated

//send 2 paramethers, funcion indicates function to be executed, and mide has the id to be updated, same of index, same of button

data: "funcion=b_cabage_list_activa&id="+mide,

//if everything worked as it should, we execute a function on client side

success: function(msg){

$("#est"+mide).html(msg); // shows what php file shows as an echo

$("#est"+mide).css("color","black"); //we change font color for another field in the same "mide" record, for example

}

});

});


Finnaly, the code for the funciones2m.php file, i store all the functions i want, and using switch selector, i use the independently one from another. This way i don't need to be uploading too many different files.



<?php

switch ($_POST["funcion"]) {

case "b_cabage_list_activa":

//actualiza el estado para el id que se le pasa

$con=mysql_connect("localhost","idsrdcom_ucab","lapassdecab");

$bd=mysql_select_db("idsrdcom_cab",$con);

$sql="update b_cabin_agenda set estado='DISPONIBLE' where id_cabage=" .$_POST["id"]." and estado=''";

$res=mysql_query($sql,$con);

//extrae el estado del registro editado

$sql1="select estado from b_cabin_agenda where id_cabage=".$_POST["id"]." and id_cabage > 1";

$res1=mysql_query($sql1,$con);

$reg1=mysql_fetch_array($res1);

echo $reg1["estado"];

break;
case "other_funcion":

//do whatever

break;
case "someother_funcion":

//do whatever

break;

}

?>


I'm not a programmer and i maked what i needed, PHPr is grate, thanks xlinesoft team.
Hope this topic be help for someone.