Jump to content

PHP and Javascript

xQubeZx

Hello,

 

I need some help with sending javascript variables to a php file so i then can upload those variables to a database.

So what i need help is how do i get my variables i have in javascript to be accessed by a php file and then added to the database.

 

This is my code right now:

<script type="text/javascript">/*  * Wrap javascript in an anonymous closure. This will limit the scope of any * functions or variables and ensure that the '$' shorthand does not collide * with any other libraries that we might be using. */(function ($, document, undefined) {/*  * Execute javascript using the jQuery shorthand for jQuery(document).ready(). * This will run the javascript when the DOM is ready. */$(function () {  /*   * Use jQuery.getJSON() to retrieve the data. Update the page HTML with   * values from the data.   */         setInterval(function(){     $.getJSON("https://dweet.io/get/latest/dweet/for/xQubeZ_temperature", function (data) {    	/*Clears old data*/	$('#thing').empty();	$('#created').empty();	$('#temperature').empty();	$('#humidity').empty();			var $thing,        $created,		$temperature,		$humidity,			    $thing = $('#thing');    $created = $('#created');    $temperature = $('#temperature');	$humidity = $('#humidity');		    $thing.text(data.with[0].thing);    $created.text(data.with[0].created);	$temperature.text(data.with[0].content.temperature);	$humidity.text(data.with[0].content.humidity);		/*Time before reset*/	}, 5000);	  });});})(jQuery, this);</script>

And the PHP:

<?php$servername = "localhost";$username = "root";$password = "passwd";$dbname = "db";// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}// prepare and bind$stmt = $conn->prepare("INSERT INTO dht (temperature, humidity, date) VALUES (?, ?, ?)");$stmt->bind_param("sss", $temp, $hum, $date);// set parameters and execute$temp = "$temperature";$hum = "40";$date = "2015";$stmt->execute();$stmt->close();$conn->close();?>

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

is there any reason that you can't decode the json with php and get them that way? It would be more efficient than what you're trying to accomplish, imo. 

 

Php has get_file_contents() and json_decode() to help with that.

Link to comment
Share on other sites

Link to post
Share on other sites

is there any reason that you can't decode the json with php and get them that way? It would be more efficient than what you're trying to accomplish, imo.

Php has get_file_contents() and json_decode() to help with that.

Could maybe do that, but iam pretty bad at php but if its possible it would work too. But if you do know a easy way (or code) to fix the problem im open for solutions

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Could maybe do that, but iam pretty bad at php but if its possible it would work too. But if you do know a easy way (or code) to fix the problem im open for solutions

Maybe take a look at this stack overflow post? It might help. I haven't played around with json too much, but if you still need help after this I can take a better look.

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe take a look at this stack overflow post? It might help. I haven't played around with json too much, but if you still need help after this I can take a better look.

It didnt help that much, dont really know how to do since i can't get a working php code that easy. (Iam Very new to php)

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

So if I get this right you have a page with that javascript code and while that is running you want it to fetch some data with that jsonGet request then forward that information to your php file to store it in the database?

Link to comment
Share on other sites

Link to post
Share on other sites

So if I get this right you have a page with that javascript code and while that is running you want it to fetch some data with that jsonGet request then forward that information to your php file to store it in the database?

Exactly, the only problem is that i dont know how to code that

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Easiest way to do that would be to use the $.post() function https://api.jquery.com/jquery.post/ and $_POST["name"] in php

 

so your js:

<script type="text/javascript">/*  * Wrap javascript in an anonymous closure. This will limit the scope of any * functions or variables and ensure that the '$' shorthand does not collide * with any other libraries that we might be using. */(function ($, document, undefined) {/*  * Execute javascript using the jQuery shorthand for jQuery(document).ready(). * This will run the javascript when the DOM is ready. */$(function () {  /*   * Use jQuery.getJSON() to retrieve the data. Update the page HTML with   * values from the data.   */         setInterval(function(){     $.getJSON("https://dweet.io/get/latest/dweet/for/xQubeZ_temperature", function (data) {    	/*Clears old data*/	$('#thing').empty();	$('#created').empty();	$('#temperature').empty();	$('#humidity').empty();			var $thing,        $created,		$temperature,		$humidity,			    $thing = $('#thing');    $created = $('#created');    $temperature = $('#temperature');	$humidity = $('#humidity');		    $thing.text(data.with[0].thing);    $created.text(data.with[0].created);	$temperature.text(data.with[0].content.temperature);	$humidity.text(data.with[0].content.humidity);	    // Post the data to the php backend    $.post(        urlToYourBackend,        {            thing: data.with[0].thing,            created: data.with[0].created,            temperature: data.with[0].content.temperature,            humidity: data.with[0].content.humidity        },        function(returnData) {            // if you want to handle what the php returns        },        'json'    );	/*Time before reset*/	}, 5000);          });});})(jQuery, this);</script>

And in the php

// set parameters and execute$temp = $_POST["temperature"];$hum = $_POST["humidity"];$date = $_POST["created"];
Link to comment
Share on other sites

Link to post
Share on other sites

 

Easiest way to do that would be to use the $.post() function https://api.jquery.com/jquery.post/ and $_POST["name"] in php

 

so your js:

<script type="text/javascript">/*  * Wrap javascript in an anonymous closure. This will limit the scope of any * functions or variables and ensure that the '$' shorthand does not collide * with any other libraries that we might be using. */(function ($, document, undefined) {/*  * Execute javascript using the jQuery shorthand for jQuery(document).ready(). * This will run the javascript when the DOM is ready. */$(function () {  /*   * Use jQuery.getJSON() to retrieve the data. Update the page HTML with   * values from the data.   */         setInterval(function(){     $.getJSON("https://dweet.io/get/latest/dweet/for/xQubeZ_temperature", function (data) {    	/*Clears old data*/	$('#thing').empty();	$('#created').empty();	$('#temperature').empty();	$('#humidity').empty();			var $thing,        $created,		$temperature,		$humidity,			    $thing = $('#thing');    $created = $('#created');    $temperature = $('#temperature');	$humidity = $('#humidity');		    $thing.text(data.with[0].thing);    $created.text(data.with[0].created);	$temperature.text(data.with[0].content.temperature);	$humidity.text(data.with[0].content.humidity);	    // Post the data to the php backend    $.post(        urlToYourBackend,        {            thing: data.with[0].thing,            created: data.with[0].created,            temperature: data.with[0].content.temperature,            humidity: data.with[0].content.humidity        },        function(returnData) {            // if you want to handle what the php returns        },        'json'    );	/*Time before reset*/	}, 5000);          });});})(jQuery, this);</script>

And in the php

// set parameters and execute$temp = $_POST["temperature"];$hum = $_POST["humidity"];$date = $_POST["created"];

Okay so i tried this and i thought it would work great, but it didn't work. I dont know why but im thinking it might be the url? How should i type it. I tried with just the name of my php file (database_insert) that is in the same folder as the index (javascript code) and i also tried with the whole address to it (192.168.0.x/WebServer/database_insert.php) but it stil didn't get the values. Any thoughts on why? 

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

I solved it, I had forgot to put the " on the url to the php lol

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×