I have a web site that periodically get a message. I put this message in a variable in javascript code (in php file). Then i want to take it in an another php page but doesn't work.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<!-- Script to import MQTT -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
</script>
<script>
....
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.destinationName);
console.log("message.payloadString:" + message.payloadString);
if (message.destinationName == "measure") {
var msg = message.payloadString;
if (msg.includes("temperature")) {
//Splitting up the message for the MAC address value
var MacVal = msg.substring(15,32);
var mac = String(MacVal);
$.ajax({
type: 'POST',
url: 'storeData.php', //url to php file
data: {mac: mac},
success: function(data)
});
}
}
</script>
In storeData.php page i have
$temp = $_POST['mac'];
But this varible is always empty.
Thanks
Related
In a WordPress post I'm trying to send data to a PHP file stored in the root folder of my website with this code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript">
console.log('hi');
var cen = document.getElementById("centro").value;
$.ajax({
url: 'centroUser.php',
type: "POST",
data: { 'cen': cen },
success: function(data){
console.log(data);
}
});
</script>
centroUser.php:
<?php
$uid = $_POST['cen'];
echo($uid);
?>
The problem is that I can't get it to work, the variable $uid doesn't get echoed and even the console.log('hi') doesn't get called. I'm new to AJAX so I don't really know what I'm doing wrong, I have tried looking for other answers but I couldn't find something that worked.
Your <script> tag has a src and a body.
Try:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
console.log('hi');
var cen = document.getElementById("centro").value;
$.ajax({
url: 'centroUser.php',
type: "POST",
data: { 'cen': cen },
success: function(data){
console.log(data);
}
});
</script>
If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI - see here.
So I am able to get my php value into javascript variable but I don't think my script.js is finding it. There of course are alot of files missing for the sake of keeping this question clean but my problem is that script.js get schoolId as undefined instead of the actual value but when I console.log schoolId i get a value of "1"(first page has id 1 so that works).
Header.php :
<?php
//gets current page id
$schoolOfficialId = $specificSchool[0]["id"];
?>
<head>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<input type="hidden" id='schoolOfficialId' value="<?php echo $schoolOfficialId;?>"/>
<script type="text/javascript">
var schoolId = $('#schoolOfficialId').val();
</script>
</body>
script.js: ajax call here NOTE: in the ajax i know the data is having trouble finding schoolId from the header.php to send to php file.
// execute an ajax query to push id of page to load_more.php
$.ajax({
url: 'load_more.php',
type: 'POST',
data: {schoolId:schoolId},
success:function(data){
console.log("working");
}
});
my load_more.php:
if (isset($_POST['schoolId'])) {
echo "yes";
}else {
echo "nope";
}//keeps echoing nope because the schoolId is not received
The problem is that you have the script running before the declaration.
<script type="text/javascript" src="js/script.js"></script> is in head, but var schoolId = $('#schoolOfficialId').val(); is at the bottom of the page.
Include the script after declaring schoolId.
<head>
</head>
<body>
<input type="hidden" id='schoolOfficialId' value="<?php echo $schoolOfficialId;?>"/>
<script type="text/javascript">
var schoolId = $('#schoolOfficialId').val();
</script>
<script type="text/javascript" src="js/script.js"></script>
</body>
P.S. despite other answers, data: {schoolId:schoolId}, is perfectly fine unless the keyname conflicts with a reserved word.
Load ajax code after defining var schoolId and also check source code whether php printing value for schoolId correctly or not and also do not forget to use $(document).ready()
<script type="text/javascript">
$(document).ready(function(){
var schoolId = $('#schoolOfficialId').val();
$.ajax({
url: 'load_more.php',
type: 'POST',
data: {schoolId:schoolId},
success:function(data){
alert(data);
}
});
})
</script>
PHP:
if (isset($_POST['schoolId'])) {
echo $_POST['schoolId'];
}else {
echo "nope";
}
By running the following i can see yes in alert ...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>stackoverflow</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script>
$(document).ready(function () {
var schoolId = $("#schoolOfficialId").val();
$.ajax({
url: 'ajax.php',
type: 'POST',
data: {schoolId: schoolId},
success: function (data) {
alert(data);
}
});
});
</script>
</body>
</html>
Ajax file (ajax.php) :
if (isset($_POST['schoolId'])) {
echo "yes";
} else {
echo "nope";
}
I want to transfer Data between the cgi page and javascript/Jquery and return back to cgi page
Index.html has the form contents
login.cgi has
my $username= $cgi->param('uid');
if (($cgi->param('uid') eq 'demo') && ($cgi->param('pwd') eq 'demo')) {
my $cookie = CGI::Cookie->new(-name=> 'uid', -value=> $cgi->param('uid'));
print $cgi->redirect(-uri => '/cgi-bin/index.cgi', -cookie => $cookie);
index.cgi has
my $uid = $cookies{uid}->value;
print $uid;
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
</body>
</html>
HTML_CODE
;
test_javascript.js has
$(document).ready(function () {
var uid = $('#uid').attr('value');
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
Otion 1 - I would hardcode it in the index.cgi. This way you can insert the value into the javascript code.
This should be your index.cgi:
$(document).ready(function () {
var uid = $cookies{uid}->value;
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
my $uid = $cookies{uid}->value;
print $uid;
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
</body>
</html>
HTML_CODE
;
Option 2 - assign $cookies{uid}->value to a hidden input and have testjavascript pick it up later
Index.cgi:
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
<input id="uid" type="$cookie{uid}->value"/>
</body>
</html>
HTML_CODE
;
$(document).ready(function () {
var uid = $.cookie("uid");
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
This is the code I have. I am trying to pull out the "data" attribute and drop it into the div at the end with id "question". I'm pretty new to JS and know there must be something really basic that I'm doing wrong, what is it?!
<!DOCTYPE HTML>
<html>
<head>
<script language="javascript" type="text/javascript">
function display() {
var base = document.getElementById("output");
var CompleteSuggestion = base.getElementsByTagName("CompleteSuggestion")[1];
var suggest = CompleteSuggestion.getElementsByTagName("suggest")[0];
var question = suggest.getAttribute("data")[0].firstChild.data;
document.getAttribute("data").innerHTML = question;
}
</script>
</head>
<body onload="display()">
<xml id="output" style="display: none;">
<CompleteSuggestion>
<suggest data="hello">
</CompleteSuggestion>
</xml>
<div class="question" id="question"></div>
</body>
</html>
Here's a better way to get the attributes from an xml file
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
//use ajax to grab the xml file
//I had an xml file named your.xml in the same directory as this script
$.ajax({
type: "GET",
url: "your.xml",
dataType: "xml",
success: function (xml) {
//when the file has loaded via the ajax request loop over each suggestion element
$(xml).find('suggestion').each(function() {
//get the value of the first attribute (data), from this suggestion row
console.log( this.attributes[0].value );
});
}
});
</script>
i am looking for a way to get title of urls mentioned in the webpage.
I tried http://urldecoderonline.com/ but it does not provide a way to parse the page.
Just for ex: user enters a url say http://www.google.com in a text input box and on pressing a submit button i would like to alert him with a title of "http://www.google.com"
Any ideas??
you need to use ajax and a php ajax proxy script on your server
i didn't test this, but it should work
ajax_proxy.php
<?php
if(isset($_POST["url"]){
echo file_get_contents($_POST["url"]);
}
?>
js
$(document).ready(function(){
$("#submit").click(function(){
$.ajax({
type: "post",
dataType: "text",
url: "ajax_proxy.php",
data: { url: $("#input").val() }
success: function(data){
alert($(data).find("title").html());
}
});
});
});
If the problem is to extract the host part from the URL string, then this is a typical regular expression problem.
If there's something before the first semicolon, that's the protocol. Everything from there up to the forward slash is the host.
Something along the line of:
<http>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function parseUrl(url) {
var regexStringProtocol = '^([A-Za-z]*)://';
var protocolResults=new RegExp(regexStringProtocol).exec(url);
if(protocolResults) {
var protocol = protocolResults[1];
url = url.substring(protocolResults[0].length);
}
else {
var protocol = '';
}
var regexStringHost = '^([^/^?]+)';
var hostResults = new RegExp(regexStringHost).exec(url);
if(hostResults) {
var host = hostResults[1];
}
else {
host = '';
}
alert('Protocol: ' + protocol + '\nHost: ' + host);
// Continue parsing if you care about the part and the parameters
}
$(function() {
$('input').val('http://stackoverflow.com/questions/6715438/parse-title-of-other-urls-using-jquery/6715527#6715527');
$('#parse').click(function() { parseUrl($('input').val());});
}
);
</script>
</head>
<body>
<input type="text" style='width:150mm' />
<br />
<button id="parse">Parse</button>
</body>
</html>
To get title of a href link you can use attr function of jQuery.
$("a").attr("title");
To get titles of all links on page:
$("a").each(function(){
var title = $(this).attr("title");
});