This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am struggling to get the following code to work. I want to take input from a javascript prompt, and output it using a php echo. When I try this I get an echo with the random numbers "12313" for no apparent reason. This is my code:
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>'
, '<script type="text/javascript">'
, 'var code = prompt("Enter verification code", "");'
, 'var getcode = code;'
, '$.post("wp-members-register.php", { code: getcode }); </script>';
$buffer_data['code'] = $_POST['code'];
echo $buffer_data['code'];
I am very new to php so please bear with me. Perhaps I am not correctly posting the 'code' variable?
EDIT: Maybe somebody can show me a better way of inputting a text string and getting it in the php code to follow? Note I am working with the wp-members wordpress plugin in the wp-members-register.php file.
Outputting javascript using PHP doesn't make any sense as javascript is a client side scripting which is executed after loading the DOM elements. PHP code is triggered when the request is sent from the browser to the server.
PHP is called before the JavaScript and cannot be called after.
You have to do everything you have to do in PHP before you use any JavaScript.
On thing you can do is use AJAX.
<?php
//php code here before the javascript
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var code = prompt("Enter verification code", "");
$.get( "wp-members-register.php?code=code", function( data ) {
document.write("Data Loaded: " + data);
});
</script>
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!
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
My aim is for a user to click a button within a row to delete that row within the database.
The button is declared as so:
<td><button class="btn btn-default" onclick="removeStudent()" button id= <?php echo $productDetails["studentID"]; ?> type="button">Delete</button></td>
The remove function is as follows:
<script>
function removeStudent() {
$id = window.event.target.id.toString();
$sql = "DELETE FROM tableStudent WHERE studentID= '$id'" ;
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error deleting";
}
}
</script>
The webpage is connected to the database, and variable names are correct and working within other functions. Just delete function not working. Any help would be much appreciated.
It looks like you are mixing javascript and PHP here. I'm not sure you understand how PHP works. Any PHP code on your page is run before the page is ever sent to the user, so you can't put PHP stuff inside javascript function (well you can, but it will be run and turned into plaintext before the javascript is ever even read by the user's browser.) So your line where you go $id = window.event.target.id.toString(); won't work, since you are trying to set a PHP variable using javascript. Actually none of that code will do anything, since its not inside a <?php block. It will just cause errors in the browser console because it is invalid javascript code.
You need to make your javascript function make an ajax call to another PHP page, which actually does the delete
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I was going to run krimaction1, a PH function when the JavaScript runs, but I've not had any luck solving this and I'm not really good at JS. How can I run that PHP function in the JavaScript?
Here is the code I've tried so far
<?php
function krimaction1() {
echo"IT IS ALIVE!";
}
?>
<script>
$(function(){
$('#krim_1').click(function(){
alert(this.id);
var myTD = this.id.split('_')[1];
var newFrm = '<form id="myNewForm1"><input name="newdata" value="' +myTD+ '" /></form>';
$('body').append(newFrm);
$('#myNewForm').submit();
});
});
</script>
I've tried another method with this as my JS:
function myFunction() {
document.getElementById("ThisIsTheValue").value = "Johnny Bravo";
document.getElementById("SubmitForm").click();
}
but that code doesn't work either for me, as when I try to set the value with the JS and click it it doesn't do anything and I can't really see what's wrong.
It seems the action of myNewForm is the php page itself, so you can add
<?PHP
function krimaction1() {
echo "IT IS ALIVE!";
}
if (isset($_GET["newdata"])) { krimaction1(); }
?>
to the page to have it execute when the form is submitted
I use $_GET here since that is the default for the form you have created.
If you do NOT want to reload the page to see IT IS ALIVE, you need to AJAX the result
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Using PHP to generate JavaScript works as expected, and using JavaScript to generate PHP works as expected, however using PHP to generate JavaScript to generate PHP does not seem to work.
Is there a problem with my code which is causing this to fail, or is it some sort of limitation?
<!-- Use JavaScript to generate PHP -->
<script type='text/javascript'>
function addComment()
{
alert("About to add some content");
document.write("<?php testFunction(); ?>");
}
</script>
<!-- Use PHP to generate JavaScript to generate PHP -->
echo "<script type='text/javascript'>";
echo "function addComment2()";
echo "{";
echo 'alert("About to add some content");';
echo 'document.write("<?php testFunction(); ?>");';
echo "}";
echo "</script>";
(testFunction() simply contains echo "Test";)
PHP is code executed on the server side. JavaScript is code executed on the client side. They don't know nothing about the other.
You can generated with PHP nearly whatever you want.
You problem is the fourth echo:
echo 'document.write("<?php testFunction(); ?>");';
You tell echo to print this string:
document.write("<?php testFunction(); ?>");
This string is sent to the client (and browser). The browser (the javascript engines) runs this code and results in an output like this:
<?php testFunction(); ?>
We have already left the php interpreter on your server, because we are already on the client. The client can't to anything with this.
If you replace your line with this:
echo 'document.write("', testFunction() ,'");';
your code will executed as expected.
But for this you have change your testFunction. Just replace
function testFunction() {
return "Test";
};
and the output to the server will be
document.write("Test");
Using JavaScript to generate PHP does NOT work. In the first case it's executed as php first, so your testFunction() is run, this resulting javascript is sent to the browser and executed.
You don't seem to understand the Server-Client model. Javascript is executed on the client, while PHP is executed on the server. The client cannot execute PHP and also cannot send PHP code to the server to be executed. It is just not possible.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to check the inputs of a form if they are empty and then i want to use php codes for user registeration.
<script>
$('#submitbutton').click(function(){
if($('#usernameinput').val() == ''){
alert('Input is blank');
}
else {
//here i want to use php codes for example;
$bgl = new mysqli("localhost", "root", "", "users");
if ($bgl->connect_errno) {
echo "Failed to connect to MySQL: (" . $bgl->connect_errno . ") " . $bgl->connect_error;
}
$username = mysql_real_escape_string($_POST['username']);
.....
.....
..... /and so on..
}
});
</script>
In this case you most likely want to use $.ajax() to call a PHP page that supplies this information.
jQuery.ajax()
There is no such thing as php and javascript working together like that. Javascript in run on client side and php is run on server side. The best you can to is to pass and retrieve values to and from a php page and then use those values as intended.
You have a few options here. The first is to post the form to a PHP page after the JavaScript validations (just use the form action) or use Ajax and call the PHP page, as Robin says.
That's not posible, you can embed javascript into php code but not vice versa.
The best way to implement that is developing a REST service and retrieving data with AJAX.