if ($page_title->exists()) {
//the rest of the code is in php.here i insert a javascript for the confirm box.
echo'<script type= "text/javascript" >
var b=confirm("This page already exists.would you want to edit");
if(b == true)
//here i want to assign a php variable , say for eg. $a to 1 so that i can use that value of $a in an if clause outside this javascript code.
</script>';
}
if($a==1)
..some code..
How do I do it?
The PHP code is executed on the server side and is all done at the point where your javascript is executed on the client side.
To communicate the value from the client to the server you typically either use AJAX (tutorial on how to do it here: http://www.w3schools.com/ajax/ajax_httprequest.asp) or store the value in a form and submit it together with data you are going to send down later anyway.
Good luck
Javascript runs on the client (browser) and PHP on your server. So you have to transport the variable to the server.
Most easy way to do this is to use the URL to send this information, for example: http://example.com/myscript.php?a=1. Then you can grab a using $_REQUEST['a'] and use it.
A more sophisticated (and complex) method is to use AJAX to send the variable to your PHP script, so it can return some data which you can use to modify your page.
What you need depends on your application, so you should provide some more details if you do not know what method is best to you in your situation.
you can send the result back to php using an ajax call, e.g.:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript">
x = confirm('Page exists, would you like to edit it?)
$.post('script.php', {x: x}, function(data) {
if (data == '1') {
alert('ok');
} else {
alert('not ok');
}
});
</script>
Related
At page 0 inputs are entered. Then Page 0 is submitted to php page 1
At php page 1 i calculate $Ama;
At php page 2 (required by page1), i need to ask one more verification check.
Hence in php page 2 :
if($Ama ==0 )
{
echo '<script>
var a = confirm("Ama is Zero. Are you sure?";
if(a){
//Set A SESSION VARIABLE
}
</script>';
}
How do i append to this a $_SESSION['Accepted']=0 where marked in Code.? I am having a problem with syntax to append this.
Since this confirm() check is done on client side, you can't run PHP code, so you will have to use AJAX to send request to server to append whatever values you want to the session.
There are many tutorials available, here is a simple way to do AJAX calls using jQuery http://api.jquery.com/jquery.ajax/
You can't manipulate a session value from Javascript, they only exist on the server.
Translate translator from Google. So that did not swear if something is not clear. Itself from Russia.
The question arose. How to pass the value of the alert in the javascript in the variable $ value in php, and write it in the case file. And another question: how to hide the alert? or use instead to visually it was not visible, but the value was passed?
//a lot of code
{
console.log(data);
alert(data['value']);
}
});
So. Also there is a PHP script that writes logs (current page and the previous one) to a file. According to this principle here:
//a lot of code
$home = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$referer = $_SERVER['HTTP_REFERER'];
$value = how the value of the java script to convey here?;
$lines = file($file);
while(count($lines) > $sum) array_shift($lines);
$lines[] = $home."|".$referer."|".$value."|\r\n";
file_put_contents($file, $lines);
It is necessary that the value of js is transferred to the php-script and write to the file. How to do it? Prompt please. I am a novice in all of this.
PHP scripts run before your javascript, which means that you can pass your php variables into javascript, but not the other way around. However, you can make an AJAX POST request from JavaScript to your PHP script, and grab the POST data in PHP through the global $_POST variable.
Assuming you use jQuery, your JavaScript would look something like:
// assign data object:
var data = { value: "test" };
// send it to your PHP script via AJAX POST request:
$.ajax({
type: "POST",
url: "http://your-site-url/script.php",
data: data
});
and your PHP script would look like:
// if the value was received, assign it:
if(isset($_POST['value']))
$value = $_POST['value'];
else
// do something else;
I'm trying to set up a webpage with Jquery that will receive button clicks from the user, pass those click values to a PHP script, that will then publish them to a MQTT broker. My connection to the broker seems to be working. I'm having problems passing variables from JavaScript to PHP. What am I doing wrong?
Here's my JavaScript:
<script>
$(document).ready(function(){
$("#button01").click(function(){$.post("post.php", {testvalue:test01});});
});
</script>
and here is my PHP:
<?php
require("../phpMQTT.php");
$testvalue = $_POST['testvalue'];
$mqtt = new phpMQTT("192.168.1.20", 8000, "client");
if ($mqtt->connect()) {
$mqtt->publish("hello/world","$testvalue",0);
$mqtt->close();
}
?>
You pass invalid JSON object to $.post() method. It should be:
{testvalue:"test01"}
So your JavaScript code should look like:
$(document).ready(function(){
$("#button01").click(function(){$.post("post.php", {testvalue:"test01"});});
});
Or if test01 is variable, it should be defined first.
Please, next time look at console in your browser and check if there is no errors and if the ajax call is sending correctly.
how do we check if table is empty with cakephp and ajax? In my index.ctp I have an image that when clicked, it will inform the user if the table is empty or not. If it's empty, an alert box will appear, and if it's not, it will be redirected to another page.
<?php
echo $this->Html->image('movie.png', array('onclick'=>'check()'));
?>
JAVASCRIPT:
function check(){
//check browser comp, create an object
object.GET("GET", url, false);
//rest of the code here
}
MoviesController.php
function index(){
//something here
$moviecount=$this->Movies->find('count');
$this->set('moviecount', $moviecount);
}
I know how to do it using the normal PHP coding, but with cakephp, and since I am new, I dont know yet. For regular PHP coding, I used the GET method for AJAX, and I can specify the URL for the PHP query inside the GET function. I don't know how to do it using cake.
You need to set the layout to AJAX then render your view. I strongly recommend not to use the index() method for this. Instead you can define a whatever() method in the MoviesController:
function whatever(){
//It is not a bad idea to do this only for GET - use the RequestHandlerComponent
$this->layout = 'ajax';
$moviecount=$this->Movies->find('count');
$this->set('moviecount', $moviecount);
}
The in the view file whatever.ctp:
echo json_encode(array('moviecount' = $moviecount));
//It is a good idea to add an isset() ternary check here like:
// echo isset($moviecount) ? json_encode(array('moviecount' => $moviecount)) : json_encode(false);
Notice that I am creating an array and encoding it to JSON. This is the way to convert variables to and from JSON. To decode use json_decode() of course.
The Client-side code really depends on what you're using to make the AJAX call but let us say that the call succeeded and you got the data back in the data variable:
//Make the AJAX call to example.com/movies/whatever via GET
//Check what data is but it should definitely be an array
if (data['moviecount']) {
//If moviecount is 0 it will go in the else statement - 0 i falsey
window.location = 'example.com/redirect/url';
} else {
alert('No records');
}
I advice against using alert() to inform the user that there are no records. Better put it somewhere in the page - in some div or whatever. Since this is an AJAX request it could be repeated many times. Consecutive use of alert() is not really user-friendly in this case.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am trying to make a confirm box which will desire which php code will be executed.
Heres my code:
<script type="text/javascript">
var answer = confirm('Are you sure?');
if(answer==true)
{
<?php $confirmation = 1; ?>
}
else
{
<?php define("CONFIRMATION", 1, true); ?>
}
alert('<?php echo $confirmation; ?>')
alert('<?php echo defined("CONFIRMATION"); ?>')
</script>
The problem is , even if i click YES, $confirmation and boolean from defined() function returns 1.
Whatever I click, (cancel or ok) one of them should be 0 (I've already declared $confirmation before)
But both of codes at if and else blocks are used!
Normally it works like this
You fundamentally misunderstand what PHP is doing.
PHP is evaluated on the server before the page is sent to your browser. By the time the browser sees it and executes the javascript, all the PHP is gone.
Use your browser's "view source" on the browser window with this code in it. You'll see it looks like this:
<script type="text/javascript">
var answer = confirm('Are you sure?');
if(answer==true)
{
}
else
{
}
alert('1')
alert('1')
</script>
You either need to implement what you want to do in javascript to run on the browser, or you need to send a new request to the server and get a new page back (either directly or indirectly) with your response.
That will never work because PHP is processed before the output is sent to the browser. If you really need to modify something in PHP then try using an AJAX call.
http://ajaxpatterns.org/XMLHttpRequest_Call
Or try using jQuery's $.ajax(); function. Start by looking here.
Here is a quick example:
<script type="text/javascript">
var answer = confirm('Are you sure?');
$.ajax({
type: 'GET',
url: '/path/to/script.php',
data: 'answer=' + answer,
success: function(response) {
alert(response);
}
});
</script>
Contents of script.php:
<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
// AJAX request
$answer = $_GET['answer'];
// ...
}
You can't trigger a PHP code exection without a post/get request.
For your needs, you should choose between a form submisssion or load a page-link with parameters stuffed in the query string on confirmation.
P.S.
the query string parameters are the ones following the "?" in the format variable=value
for example:
index.php?answered=1
you will be then able to retrieve these vatiable/values using PHP $_POST, $_GET or $_REQUEST variables in a way like this:
if ($_REQUEST['answered'] == 1) { //confirmed
...
}
You are misunderstanding the order of what will happen here.
Firstly, PHP will output the javascript layer. Your if block will then look like this:
if (answer == true)
{
}
else
{
}
The javascript engine should then optimise that out and totally ignore it. Consider using AJAX if you need to get PHP to process something with an input from the javascript layer.
Normally it works like this
No, it never works like this. PHP is executed before the javascript so it will never work like this.
I think from what I see you would want something like
Your link
This will go to the current page with $_GET['confirmation'] set to "1".
php executed before javascript so you can't do this because
when you check it via javascript if else statement php is already executed so you can't do it
but however you can use ajax for it