This topic is locked
[SOLVED]

 Add page fields values to external API

3/6/2017 3:42:27 AM
PHPRunner General questions
S
snuffi01 author

Hi,
I need some advise how to be able to use values from fields on an "add" page from button (use it with external API).
I use PHPRunner to show data from postgresql and mysql db, when user wants to add anything i cant do it in the db, i have to use an API to send new data and updates.

I still want to use PHPrunner as it makes it easy in other part of my project.

My problem is how to get the values that user enters inside fields on the add page.
As example i have an table with this fields:



presence_extension,

presence_cause,

presence_mood,

presence_endtime,

presence_date


My plan is to make an new button that on "Client before" fetches the values and on the "server" send to API.
This is what i've been trying:

"Client before"



var ctrlExtension = Runner.getControl(pageid, 'presence_extension');

var valueExtension = ctrlExtension.getValue();
var ctrlPresence = Runner.getControl(pageid, 'presence_cause');

var valuePresence = ctrlPresence.getValue();
var ctrlMood = Runner.getControl(pageid, 'presence_mood');

var valueMood = ctrlMood.getValue();
var ctrlSluttid = Runner.getControl(pageid, 'presence_endtime');

var valueSluttid = ctrlSluttid.getValue();
var ctrlDatum = Runner.getControl(pageid, 'presence_date');

var valueDatum = ctrlDatum.getValue();


"Server tab"


require_once ("saresources/included_files/sa_api.php"); /External API
if ($valuePresence == 'Tillgänglig') {

$data[extension_id] = $valueExtension;

$data[mood] = 1;

}

else {

$data[extension_id] = $valueExtension;

$data[status] = join(" ", array($valuePresence, $valueDatum, $valueSluttid));

$data[mood] = $params["mood"];

}

$api = new sa_api();

$json = $api->post('api/set_extension_status', $data);

return true;


With above code i dont get the values inside all th efields on the add page, what am i doing wrong?
/ Kristian

Sergey Kornilov admin 3/6/2017

What exactly "don't get the values inside all the fields on the add page" means? You getting some but not getting all?

S
snuffi01 author 3/6/2017



What exactly "don't get the values inside all the fields on the add page" means? You getting some but not getting all?


Hi, i get no values at all.

Sergey Kornilov admin 3/6/2017

Which part of your code is responsible for sending those field values to API?

S
stiven 3/7/2017

Have you looked at this topic?
https://xlinesoft.com/phprunner/docs/custom_add.htm
maybe what you are looking for is a custom add. when the users clicks save on PHPrunner application you can get the values entered on the events page for add records. No need to create a button.. or if you do need the button then there are other ways to achieve that.

S
snuffi01 author 3/7/2017



Which part of your code is responsible for sending those field values to API?


Hi,

i hav eleft out that part,

"Server tab" sends to sa_api.php and that file look like this:



<?php

class sa_api

{

var $headers;

var $user_agent;

var $compression = 'gzip';

var $cookie_file;

var $proxy;

var $proto = 'http';

var $server = 'localhost';
function sa_api()

{

$this->headers[] = 'Accept: application/json';

$this->headers[] = 'Connection: Keep-Alive';

$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';

$this->user_agent = 'SA_WebGUI';

}
function post($uri, $data_as_array)

{
$process = curl_init($this->proto . "://" . $this->server. "/" .$uri);

curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);

curl_setopt($process, CURLOPT_HEADER, false);

curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);

curl_setopt($process, CURLOPT_ENCODING, $this->compression);

curl_setopt($process, CURLOPT_TIMEOUT, 30);

if ($this->proxy)

curl_setopt($process, CURLOPT_PROXY, $this->proxy);

curl_setopt($process, CURLOPT_POSTFIELDS, $this->stringify_array($data_as_array));

curl_setopt($process, CURLOPT_RETURNTRANSFER, true);

curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($process, CURLOPT_POST, true);

$return = curl_exec($process);

$err_status = curl_error($curl);

$http_status_code = curl_getinfo($process, CURLINFO_HTTP_CODE);

curl_close($process);
if (strlen($err_status) > 0)

return $this->error("curl reported a error: $err_status");
$json = json_decode($return);

if($json == null){

$constants = get_defined_constants(true);

$json_errors = array();

foreach ($constants["json"] as $name => $value) {

if (!strncmp($name, "JSON_ERROR_", 11)) {

$json_errors[$value] = $name;

}

}

return $this->error("json_decode reported a error: " . $json_errors[json_last_error()]);

}
if($http_status_code != 200)

return $this->error("request failed $http_status_code:$json->message");

return $json;

}
private function stringify_array($array)

{

$encoded = array();

foreach($array as $key=>$value) {

$encoded[] = $key .'='. urlencode($value);

}

return join("&", $encoded);

}

private function error($error){

trigger_error($error, E_USER_ERROR);

return null;

}

}

?>


I know that there is a way to do this "before record added" with code like this:



require_once ("saresources/included_files/sa_api.php");

//Test att skicka special om tillgänglig är valt

if ($values["presence_cause"] == 'Tillgänglig') {

$data[extension_id] = $values["presence_extension"];

$data[mood] = 1;

}

else {

$data[extension_id] = $values["presence_extension"];

$data[status] = join(" ", array($values["presence_cause"], $values["presence_date"], $values["presence_endtime"]));

$data[mood] = $values["presence_mood"];

}

$api = new sa_api();

$json = $api->post('api/set_extension_status', $data);

return true;

document.location.reload(true);



But my problem is that i cant save to the db so i would like to make an new button that sends th evalues to my api (sa_api.php).

S
snuffi01 author 3/7/2017



Have you looked at this topic?
https://xlinesoft.com/phprunner/docs/custom_add.htm
maybe what you are looking for is a custom add. when the users clicks save on PHPrunner application you can get the values entered on the events page for add records. No need to create a button.. or if you do need the button then there are other ways to achieve that.


Hi, thanks for your answer.

I've been trying custom add, but my problem is that i cant save to db in this case, so i need a way to send values from the fields and not doing the add to db thing.

I'm very interesting if there are other ways to achive this!!!

Sergey Kornilov admin 3/7/2017

You need to read this article one more time:

https://xlinesoft.com/phprunner/docs/custom_add.htm
If you return false record won't be added to the database.

S
snuffi01 author 3/7/2017

Thanks,

I have read the "custom add" and it seems to be exactly what i need.
Now when i try to use i run in to problem...
I have this on "custom add"



require_once ("saresources/included_files/sa_api.php");
if ($values["presence_cause"] == 'Tillgänglig') {

$data[extension_id] = $values["presence_extension"];

$data[mood] = 1;

}

else {

$data[extension_id] = $values["presence_extension"];

$data[status] = join(" ", array($values["presence_cause"], $values["presence_date"], $values["presence_endtime"]));

$data[mood] = $values["presence_mood"];

}

$api = new sa_api();

$json = $api->post('api/set_extension_status', $data);
return false;


The code is executed and my external api gets the values just as i want <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=81545&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
BUT, after code is executed i get error in the webpage (ERROR: lastval is not yet defined in this session)

Error can be found here: http://pastebin.com/Y3HJUsXp
What am i missing, something wrong with my code above?