My problem:
<div id=mahashh></div>
<script>
var $mahash = window.location.hash.replace('#', '');
document.getElementById("mahashh").innerHTML = "Your ID " + $mahash;
</script>
<input type="text" id="mahashh" name="mahashh" value ="<?php echo $mahash ?>" >
Value Your ID shows correctly but input value is not shown.
Somebody help me show it in Input value. Thanks so much !
I'm newbie, not pro ^^!
In
<?php echo $mahash ?>
you are trying to display value previously set in
var $mahash = window.location.hash.replace('#', '');
and the reason of why it cant work is: php is a server-side language and javascript is a client-side lannguage. It means that php code is executed on different machine (imagine aws server) but javascript code is executed in your browser (your pc, not aws server). So, php and js can't communinate directly between each other.
A couple of things:
PHP is a server-side language. Javascript (not Java) is a client-side language. You're trying to output a Javascript variable into PHP which is not possible (at least, not in the way you're trying to do it)
A hash value from a URL is not processed by the server, and is therefore not available by default in PHP. Trying to send a hash fragment to the server from Javascript would involve something like AJAX, which is not likely what you want.
Without fully knowing your exact use-case here, you might be better off using Query Parameters to pass simple information to the server.
As an example:
// http://yourlocalsite.com?id=yourId
<div id="mahashh"><?php echo 'Your ID ' . $_GET['id'] ?></div>
<input type="text" id="mahashh" name="mahashh" value ="<?php echo $_GET['id'] ?>" >
That's a pretty simple (and rather insecure) example, but should give you a better start on what you're attempting to accomplish.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
Actually, I have a javascript variable I pass this to PHP variable and use this variable as a MySQL query when I submit the query the page gets reloaded and value of the javascript variable is finished and therefore query doesn't work
I just want a modification in my code or there is another relevant solution regarding my problem then kindly please help me.
Everything works fine when echo the PHP variable it shows me the variable only problem is in the query of my SQL that in the query the PHP variable which has javascript variable is not working.
<script>
var buttontext="esteem";
<?php
$ff ="<script>document.write(buttontext);</script>";
?>
</script>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="beelist";
$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'beelist');
if($_POST['sub'])
{
echo $ff;
$code=$_POST['Bid'];
if($code!=""){
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '$ff'";
$data = mysqli_query($conn,$query);
$res1=mysqli_fetch_array($data);
if ($res1) {
echo '<script> alert("Beacon found")</script>';
echo '<script> showp();</script>';
}
else {
echo '<script> alert("Beacon ID is wrong!")</script>';}
}else{
echo '<script> alert("Beacon ID is required")</script>';
}
}
?>
As I said in the comments
Where do I start, Client and Server (JS and PHP) are separate. One runs on the server one runs on the clients computer. PHP goes first and as such only PHP can affect JS (as that is part of the output of the PHP) the JS's state cannot be known in PHP as it's on a entirely different computer. Basically you are left with making a request, either from a page reload (such as form submission) or AJAX where you can pass that data back to the server so it can work on it.
Basically what you have now is $ff literally is this text:
$ff ="<script>document.write(buttontext);</script>";
And because you don't echo it, it's actually never passed to the Client as part of the source.
Your query is like this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '<script>document.write(buttontext);</script>'";
It's too broad of a topic for me to really give you a working answer, and there are plenty of tutorials out there that can do it better justice them me. But hopefully, you understand what is going on now.
PS. you can test this easily by doing echo $query; right after the query. Also be aware you should not put PHP variables directly in SQL, or you risk SQLInjection type attacks against your site. For example if $_POST['Bid']="' OR 1 -- your query would be this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '' OR 1 --' && name = '<script>document.write(buttontext);</script>'";
Where -- is the start of a comment in MySQL. So now I just selected your entire table by injecting SQL commands into your Query, which is a very bad thing.
Cheers!
I am trying to save a variable for counting the views of my personal website, I dont need to use php because its literally a viewcount. I know how to retrieve the count from the server using $.post, but how would I retrieve it (Edit: In the simplest way possible.)?
The website I'm trying to do it with is http://artsicleprojects.com/
Thanks in advance!
You will need PHP for this question, because it is dealing with server-side actions. First, you need to make a server-side script to increment the text file's number. Then, you will need to make a client-side script to make a request to the server. This script increments a number in the text file every time the request is made. Anyway, here's how I would do it (Note: this code is un-tested):
PHP:
<?php
/*Reads and collects current count.*/
$rfile = fopen("views.txt", "r") or die("Unable to open file!");
$count = fread($rfile,filesize("views.txt"));
fclose($rfile);
/*Increments the count.*/
$wfile = fopen("views.txt", "w");
$ncount = $count + 1;
fwrite($wfile, $ncount);
fclose($wfile);
?>
Note on code: for this code to work correctly, you may need a text file already made (views.txt), in the same directory as the PHP script, with a single "0" written in it.
JavaScript (with jQuery):
$.post("phpscript.php", function(data, status){
console.log(status);
});
This also is supposed to be in the same directory as the script to work.
I would like to push JavaScript value into php:
var temp = $('#field_id').val().charAt(0);
temp returns values 1-4.
var value = "<?php echo $variable[ ..... ]['id'] ; ?>";
how to insert instead .....
Its impossible, the main reason is that Php is rendered server side ( so before it arrives to the client computer), while JavaScript is rendered in the browser ( so in the client PC).
Depending from your requirement, you may use an ajax request to get the info and update the dom with javascript, or use GET or POST request with the js variable required so that PHP will have it before it renders the page.
If you wold give me more information, I may probably help you, but the question is not specific enough
I am using a plugin userfrosting to manage users.
The PHP side of the app checks if the user is currently logged in.
The javascript side of the app checks what the permission of the user is.
EG.
<?php
if (isUserLoggedIn()){
?>
<script>
loadCurrentUserPermission();
</script>
<?php
}
?>
Now that's fine and dandy, but now I want to implement some user access restrictions in my PHP files. Can I use such a method inside of a php function? Because I don't want to escape the PHP and check for every action.
The next option would be to store it in a variable, but how can I escape a variable assignment midway to the outcome of a < script > ?
$userPermission;
if (isUserLoggedIn()){
$userPermission = (
?>
<script type="text/javascript">
userLoadPermissions();
</script>
<?php
);
}
?>
Finally what if I use jquery to store the permission level in a PHP session ID? is that safe? Can someone easily modify their own level?
What's wrong with echoing the js?
<?php
if (isUserLoggedIn()){
echo "<script>loadCurrentUserPermission();</script>";
// more php
?>
Edit: Oh, I see. Javascript, being server-side, will be loaded after the PHP, so it's not simple (or even possible) to have PHP, then JS, then more PHP execute on the same page. You could consider calling the rest of the PHP from somewhere else using an AJAX request but that might not be practical for all of your pages.
Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;