JAjax function to copy a string into a mysql record

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-03 13:26

Hello,
I need your help because, after a lot of time, testing different way, I have not found a solution to my problem.
Summarising, on my Appgini application I have a field called wid. It is a nyumber string.
I have to copy it inside a record in the same table in teh same db.
To do this I made two file:
1) Javascript file with Jquery AJAX function to read and copy the string
2) php file to receive the string and save it in the record.
These are the two files:
js:
function copy(){
var wid = document.getElementById("wid");
$j.ajax({
type : 'POST',
url : 'reqhr.php?wid='+wid,
data: {'wid': ("wid")},
success: function(data) {
if (data == 'ok'){
alert('all good');}
else{
alert ('something wrong');
}
}
});

console.log(data)
console.log(wid)
}
{
$j('#command').val($j('#wid').text().trim());
}
$j(function() {
$j(
'<div class="btn-group-vertical btn-group-lg vspacer-lg" style="width: 100%;">' +
'<button id="bc" onclick="copy()" type="button" class="btn btn-primary btn-lg">' +
'<i class="glyphicon glyphicon-list-alt"></i> Check Heart Rate' +
'</button>' +
'</div>'
).appendTo('.detail_view .btn-toolbar');
});

php:
<?php
$code=",08073#";
$codeheart="ACVBPXL,";
$servername = "localhost";
$database = "rsnms";
$username = "nms";
$password = "xxx";
$wid = $_POST["wid"];

if (isset($_POST["wid"])) {
$wid = $_POST["wid"];
}
// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);

echo $codeheart."".$wid."".$code;
echo $codeheart."".$code;
echo $wid
?>

The js application finds the wid value but doesn't send anything to the PHP file.
Coudl you help me please to understand where I'm making a mistake?
Thank you very much indeed
Have a nice day

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-03 21:18

Hi,

your

var wid = document.getElementById("wid");

gets the object, but not the value.

Try

var wid = document.getElementById("wid").value;
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-04 10:35

Thank you for your answer.
The problem is the wid it is a variable. It is not a fixed value
How can I solve this?
Thank you for your help
Davide

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-04 16:08

Hi,

I would have assumed that it is a variable, why would you try to read it's value otherwise.
So where is your problem? You have a button to call the copy js function. That one would read the variable and call the php via ajax.

your ajax call is a bit strange as you do a mixed post and get request.
Also the way you pass the data is strange

type : 'POST',
url : 'reqhr.php?wid='+wid,
data: {'wid': ("wid")},

try

type : 'POST',
url : 'reqhr.php,
data: {wid: wid},
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-04 16:29

Thank you my friend I did this and the result at the end is OK about the js but nohing arrives ti the PHP.
echo $wid = nothing :-(
What can I do??
I tried a lot of different configurations about js and PHP but always teh same result.
Thank you

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-04 16:46

Hi,

try
JS:
function copy() {
var wid=$j('#wid').val();
$j.ajax({
type : 'POST',
url : 'reqhr.php',
data: {wid: wid},
success: function(data) {
alert(data);}
});
}

PHP: reqhr.php

<?php
echo $_POST['wid'];

this should alert the data that is in wid.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: JAjax function to copy a string into a mysql record

Post by D Oliveira » 2020-04-04 20:31

pböttcher wrote:
2020-04-04 16:46
Hi,

try
JS:
function copy() {
var wid=$j('#wid').val();
$j.ajax({
type : 'POST',
url : 'reqhr.php',
data: {wid: wid},
success: function(data) {
alert(data);}
});
}

PHP: reqhr.php

<?php
echo $_POST['wid'];

this should alert the data that is in wid.
worth noting if you use that variable to insert a value in the database always use makeSafe() function to prevent SQL vulnerability

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 07:59

Hi,
thank you for your answer,
unfortunatley the resul on js is a empty window on alarm window.
Using this:
function copy(){
var wid = document.getElementById("wid").value;
$j.ajax({
type: 'POST',
url: 'hooks/reqhr.php',
data: {wid: wid},
cache: false,
success: function(copy){
alert(wid);
}
});
}
I have the wid value inside the alarm window, but if it is the data the value sent. This is the reason because I have nothing in the php file
;-( .-(

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 08:29

Maybe I'm writing a stupid thing but...
If I use the javascript to make the buttons and I use PHP for everything else??

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 09:09

Hi,

just try the code I gave you. Don't modify it.
your code will of course not outpout anything as the wid is not defined within the success function.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 09:16

I tried yours and the result is a blank window in the js part
to be exact . Nothing happens no window appear and nothig in th ephp part as well :?

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 09:22

I modified the js addind a directory in the js file like this:
function copy() {
var wid=$j('#wid').val();
$j.ajax({
type : 'POST',
url : 'hooks/reqhr1.php',
data: {wid: wid},
success: function(data) {
alert(data);}
});
}
console.log(wid)
$j(function() {
$j(
'<div class="btn-group-vertical btn-group-lg vspacer-lg" style="width: 100%;">' +
'<button id="bc" onclick="copy()" type="button" class="btn btn-primary btn-lg">' +
'<i class="glyphicon glyphicon-list-alt"></i> Check Heart Rate' +
'</button>' +
'</div>'
).appendTo('.detail_view .btn-toolbar');
});
and now the window with wid is appeared but nothing happens in the PHP part

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 09:32

after restarting of everything, the resut is:
blank window on JS and nothing on reqhr1.php

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 10:01

Hi,

what is your reqhr1.php? What would you expect it to do?

So currently if you click on the "Check Heart Rate" button a blank pop-up appears, correct?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 10:35

Yes it is correct. I need to copy the wid value on reqhr1.php
I have to merge three different values to make a string command inside the string field.
two of them are always the same but wid depends by the patient.
My final objective is, using reqhr1.php to save the final string composed by the three values inside the string field
Using the complete path on url the alarm window has the wid value inside so it is right, but nothing arrives to req1.php
Last edited by dgasparin on 2020-04-05 10:39, edited 1 time in total.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 10:38

can you post the reqhr1.php
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 10:40

sure
it is yours
<?php
echo $_POST['wid'];
?>
you can find here the result using the complete path
Attachments
result.PNG
result.PNG (96.87 KiB) Viewed 7971 times

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1814
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: JAjax function to copy a string into a mysql record

Post by jsetzer » 2020-04-05 10:51

You will have to...

echo json_encode($data);
exit();

...in php file
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 10:56

I cannot see the wid in your screenshot, which value is it that you copy in that example?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 11:04

The wid is the value that you see inside the window

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 11:13

So everything worked fine. on button click you get the wid value.

So within your reqhr1.php you can now use that value $_POST['wid'] to do what you need.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 15:02

I tried but nothing happens in reqhr1.php
usng this:
<?php
echo $_POST['wid'];
?>
the result is nothing

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-05 15:14

sorry i forgot a line:
<?php
$wid =$_POST['wid'];
echo $wid;
?>
The result is always nothing

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: JAjax function to copy a string into a mysql record

Post by pbottcher » 2020-04-05 20:05

Hi,

it just does what it shall do. You call via an ajax function a php script to echo the wid, which is then captured in the success of the ajax call and displayed in the alert box.

My final objective is, using reqhr1.php to save the final string composed by the three values inside the string field
Using the complete path on url the alarm window has the wid value inside so it is right, but nothing arrives to req1.php

So the question is, what are your 3 values that you want to combine to a string and update your database accordingly.
Are you trying to add the data to an exisiting table of your database that holds the app?

You would need to pass the ID (PK) of the record you wand to update to the reqhr1.php file as well, so that you can access the correct record.
Maybe you can post the variables you try to combine and the information of the table where you want to update the data. Then I can try to help further.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dgasparin
Veteran Member
Posts: 31
Joined: 2020-03-26 13:03

Re: JAjax function to copy a string into a mysql record

Post by dgasparin » 2020-04-06 06:10

Thank you so much my friend, I really apprciate your help.
The table name is patients and the three fields to concatenate are:
codeh,wid,code
all of them have to be wrote in the same table on command field
That's all
Thank you so much

Post Reply