Check if magento shopping cart is empty using javascript? - javascript

So I need to check if the cart on my magento site is empty or not.
I can do this using php like this:
<?php
$count = $this->helper('checkout/cart')->getSummaryCount();
if($count==0){
echo 'Order';
}
else {
echo 'CHECKOUT';
}
?>
However the problem is that using php it will get cached. So in order for the button to change you'd have to refresh the cache.
So I was wondering if there is any way to do this check in javascript instead of php as I can't think of a way to do it?

You could use AJAX to run a asynchronus php request (should be uncached) to get the state of your cart. With the result of that request you can work in javascript to change the state of your button.
You can find more info about ajax and how to use it here:
How does AJAX work?
Ajax tutorial for post and get
https://www.atwix.com/magento/ajax-requests-in-magento/

Related

Wordpress get post id with a php file

I have been stuck on this for weeks. I have an HTML slide presentation using Reveal.js. I want to run a php script on the very last slide.
This is an HTML button on the last slide within a Wordpress post:
<button onclick="myFunction()">Open php file</button>
<script>
function myFunction() {
window.open("../../testing/test.php", "_self");
}
</script>
This is the code inside the test.php file that I am trying to run but it returns null:
define('WP_USE_THEMES', false);
require_once('../wp-load.php');
echo "Post ID: ".get_the_ID(); // Returns nulls but I want this to return 86.
The post id is 86. I can hard code it into the html (if I have to) but I don't want to hard code it into the php file. Also, I would prefer not to use jquery. How can I get the post id into the php file? Thanks.
Have you tried using global $post variable??
global $post;
And after that, you can get the id,
echo "Post ID: ".$post->
Another approach is using $wpdb global variable to make database queries.
You can also use JavaScript to send your post id to another php file using javaScript forms, Ajax or jQuery. jQuery Post.
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

Edit Session Through PHP Script

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.

Checking if a user is logged in with PHP and javascript

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.

whether to use php or javascript method for redirecting a page

Whenever I user php header("Location: index.php"), most of time then it does not work. It exactly DOES NOT work without any error nor any run-time fatal error. I don't use any echoing nor I keep empty lines prior to header() call. I double check everthing, but it does not work.
Now, I user JS solution. I write a function that gets the param, store it in an immediately created <input> field and the using Javascript in the same function, I get the value out of input and assign it to window.location.href= value; which then finalizes the redirection. Is my method reliable and good?
My function seems like this:
function redirect($address)
{
?>
<input id='this_address' value="<?php echo $address; ?>" />
window.location.href = document.getElementById("this_address").value;
<?php
}
Are you sure that your index.php is in the same directory as the scripts that calls redirect or vice versa...?
If not try header("location:../../index.php");
Setting and cookies or session variables will cause issues with a redirect. It has to be sent before any output or exchange with the server and the client. Check your browser log for any errors too. It might be helpful in determining why you are not achieving the result you want.
You could also implement both methods, but it would be more useful to find out why your PHP redirect is not effective.
If u can't able to find out the solution for php header prob, Just add the below code at the top of the page. This is not the perfect way, but we can use this...
ob_start();
Why not use Response.Redirect "../../index.php" to redirect to another page.

How to check if table is empty using cakephp and AJAX?

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.

Categories

Resources