Writing javascript in PHP - javascript

I am new to PHP, I just need to write a javascript code inside php file like we doing in jsp.
Below is the sample code where am trying to do that, Please help me out to embed my javascript code in php
getencryp.php
<html>
<body>
<script type="text/javascript" src="js/require.js"></script>
<script type="text/javascript" src="js/jsencrypt.js"></script>
<script type="text/javascript">
var b=getAccountbyEncrptInfo('xxx','yyyy')
function getAccountbyEncrptInfo(user,pass)
{
var omegaKey = 'abc';
var crypt = new JSEncrypt();
crypt.setPublicKey(omegaKey);
var creds = {
username: user,
password: pass
};
creds.username = crypt.encrypt(creds.username);
creds.password = crypt.encrypt(creds.password);
return JSON.stringify(creds);
}
</script>
<?php
echo Your encrypted username and password is $b;
?>
</body>
</html>
In the above code am just passing two values in java script getAccountbyEncrptInfo('xxx','yyyy') and just trying to print inside php body.

b is a variable in javascript and it's not a variable in PHP. So that you can't echo it in the PHP tag.
I think you should implement the function getAccountbyEncrptInfo in PHP code or binding the variable from javascript into the html tag instead of using echo in PHP. It should be something like:
<html>
<body>
<script type="text/javascript" src="js/require.js"></script>
<script type="text/javascript" src="js/jsencrypt.js"></script>
<script type="text/javascript">
var b=getAccountbyEncrptInfo('xxx','yyyy');
document.getElementById('encrypted').html(b);
function getAccountbyEncrptInfo(user,pass)
{
var omegaKey = 'abc';
var crypt = new JSEncrypt();
crypt.setPublicKey(omegaKey);
var creds = {
username: user,
password: pass
};
creds.username = crypt.encrypt(creds.username);
creds.password = crypt.encrypt(creds.password);
return JSON.stringify(creds);
}
</script>
<div id="encrypted"></div>
</body>
</html>

Why do you need to use PHP?
<script>
var b = getAccountbyEncrptInfo('xxx','yyyy');
document.getElementById('ex').append(" Your encrypted username and password is " + b)
</script>
However if you wanted to append it inside the <?php ?> tags to further process your data, you might want to consider using an AJAX request.

just add this line after var b declaration
$('#answer').html(b);
and replace
echo Your encrypted username and password is $b;
this line with this line
Your encrypted username and password is div id="answer"></div
just make a blank div and set it`s id answer so js is set the value on it

Related

Javascript and HTML button to INSERT MySQL query

I can't use AJAX/PHP solution to upload some data to MySQL database by clicking html button so I created .js function.
upload.js:
function uploadLikes(post) {
// credentials
var pass = require("/home/user/auth.json");
var passString = JSON.stringify(pass);
var passJson = JSON.parse(passString);
var mysql = require("mysql");
var connection = mysql.createConnection({
host : passJson.bi_anr.host,
user : passJson.bi_anr.user,
password : passJson.bi_anr.password,
});
// source: https://stackoverflow.com/questions/5818312/mysql-with-node-js
connection.connect(function(err) {
// connected! (unless `err` is set)
});
var query = connection.query("INSERT INTO db.likes (max_hash, ts, likes) VALUES (?, unix_timestamp(now() ), 1 ) ;", post, function(err, result) {
// Neat!
});
//console.log(query.sql);
}
and according to THIS I created button with javascript execution:
HTML file:
<html>
<head>
<title>Like Button</title>
</head>
<body>
<script src="upload.js"></script>
<input id="clickMe" type="button" value="clickme" />
<script>
//- Using a function pointer:
document.getElementById("clickMe").onclick = uploadLikes('TEST');
</script>
</body>
</html>
but it does not work. JavaScript function is OK, because it works by executing it in terminal using node. Could you help me?

How can i use the variable which has been set up by another api, in my javascript function?

I am trying to get the ip of the user via the api https://l2.io/ip.js?var=userip
I can see the value of userip in the same script, but when I try to send this ip to my webpage to save it in my database, this variable is undefined.
Here is my html and javascript:
<script type="text/javascript">
var userip;
</script>
<script type="text/javascript" src="https://l2.io/ip.js?var=userip" >
</script>
<body onload="whichButton(event)">
<script>
function whichButton(event)
{
makeRequest('http://example.com/git.php?userip=' + userip);
}
<script>
The thing which is stored in my database is "undefined".
How can I send the variable which has the ip stored in it?
I am new in javascript. Hope you won't mind.Thanks
<script>
var userip;
makeRequest('https://l2.io/ip.js?var=userip');
function whichButton(event)
{
makeRequest('http://example.com/git.php?userip=' + userip);
}
</script>

Javascript scope and sequentiality issue

I want to get an id from the query and display it in html.
I want to keep things in separate javascript files, because in the future, it will retrieve a json from php and will filter it based on some checkboxes, radio buttons, etc. and then display the result.
In this moment, I get a blank page, and the id value is not shown, so I'm doing something wrong.
http://localhost:10600/js1/index.php?id=5
index.php
<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
</head>
<body>
<div class="results"></div>
</body>
</html>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="display.js"></script>
<script type="text/javascript" src="sequence.js"></script>
config.js
function getId() {
id = "<?php echo $_GET['id']; ?>";
}
display.js
function updateHtml() {
window.onload = function() {
document.querySelector('.results').innerHTML = id;
}
}
sequence.js
function seq()
{
getId();
updateHtml();
}
seq();
Please try the following:
index.php
<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
</head>
<body>
<div class="results"></div>
</body>
</html>
<script type="text/javascript">
var id;
function getId() {
id = "<?php echo $_GET['id']; ?>";
}
</script>
<script type="text/javascript" src="display.js"></script>
<script type="text/javascript" src="sequence.js"></script>
function getParameters() {
var url_params = window.location.search.substring(1); // Removing characters : ?
var params = url_params.split("&");
var res = {};
for (var i = 0, c = params.length; i < c; i++) {
var split = params[i].split("=");
res[split[0]] = split[1];
}
return res;
}
Returns an object {param: value}
In index.php within body you can do like this one
<input type="hidden" name="hidden_id" id="hidden_id" value="<?=$_GET['id']?>">
In JS :
If you are using jQuery,then you should do like this one.
$(document).ready(){
var strHiddenId = $("#hidden_id").val();
$(".results").html(strHiddenId);
}

Javascript src with variable

I need to embed a player by the following code:
<script type="text/javascript" src=***the url***></script>
A part in the url is the ID which is retrieved from mySQL database. I know that the string concatenation can't be directly handled in the src, so I add the function in the beginning:
<head>
<script>
function call(url)
{
var script = document.createElement('script');
script.type="text/javascript";
script.src = url;
document.body.appendChild(script);
}
</script>
</head>
In the body,
<body>
<?php ***...to retrieve ID***?>
<script type="text/javascript">
var videoid = "<?php echo $id; ?>"; //Transfer ID to javascript
var url="http://......"+videoid; //Url string finished
call(url); //Function call
</script>
Now the script is appended, but I can't see the player. Why?

Next and previous button using jquery

I am using a PHP Simple HTML DOM Parser to save a website as a htm file. I then use jQuery to extract a div from the file and put it into a div. I want to create a
Previous and Next button but the problem I have is that to do this I have to change the URL so the parser can get the page. I would really appreciate if you can help. Below is the code I am using.
<?php
include( 'site/simple_html_dom.php');
$html=file_get_html( 'http://roosterteeth.com/home.php?page=1');
$html->save('site/rtnews.htm')
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
$('document').ready(function() {
$('#wrap').click(function (event){
event.preventDefault();
});
$("#wrap").load('site/rtnews.htm #postsArea');
});
</script>
</head>
<body>
<div id="wrap">
</div>
</body>
You will have to create a new php file for this and make an AJAX request to that file. I assume you have already realised that you cannot make a cross-domain request due to CORS.
Here is your new php file, let's call it proxy.php. It will proxy the request, responding with the page that is passed to it via GET :
<?php
include( 'site/simple_html_dom.php');
$html=file_get_html( 'http://roosterteeth.com/home.php?page=' . $_GET["page"]);
echo $html;
?>
Your new JavaScript;
$('document').ready(function() {
var $wrap = $('#wrap'),
page = 1;
$('#next').on('click', function () {
getPage(++page);
});
$('#prev').on('click', function () {
getPage(--page);
});
var getPage = function (page) {
$wrap.load('proxy.php?page=' + page + ' #postsArea');
};
getPage(page);
});

Categories

Resources