Jump to content

Archived

This topic is now archived and is closed to further replies.

Pavlos1316

<add to cart> link help

Recommended Posts

hello,

 

I tried if(isset($_POST['update'])) conditional but not only it doesn't open the cart in the same div BUT... it doesn't update the cart either...

 

:confused:

 

Thank you...

 

I am going to jump to something that seems easier for the moment since I spent a lot of time on this one.... I will come back to it later with clear mind..!

 

ok...  I have this code to show the number of items in the shopping cart:

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return 'no items';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return ''.count($items).' item'.$s.'';
}
}
?>

I wanted to call that fanction inside my <div id="header"> and I did. The problem is that when I add an item even that it is added in the cart I need to refresh the page to show it in the header div. How can I make it shown there when I click the add button?

 

This is my add button:

<a href="javascript:ajaxpage('cart.php?action=add&id=1', 'null');" class="adcart">Add to cart</a>

 

Thank you

Share this post


Link to post
Share on other sites

hello,

 

I tried if(isset($_POST['update'])) conditional but not only it doesn't open the cart in the same div BUT... it doesn't update the cart either...

 

:confused:

 

Thank you...

 

What does your PHP script look like?  How are you using your ajax?

 

I am going to jump to something that seems easier for the moment since I spent a lot of time on this one.... I will come back to it later with clear mind..!

 

ok...  I have this code to show the number of items in the shopping cart:

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return 'no items';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return ''.count($items).' item'.$s.'';
}
}
?>

 

I wanted to call that fanction inside my <div id="header"> and I did. The problem is that when I add an item even that it is added in the cart I need to refresh the page to show it in the header div. How can I make it shown there when I click the add button?

 

This is my add button:

<a href="javascript:ajaxpage('cart.php?action=add&id=1', 'null');" class="adcart">Add to cart</a>

 

Thank you

 

I thought you were going to get rid of ajaxpage() and move on to jQuery?

 

In any event, you need to modify your PHP script in order to have it call your writeShoppingCart() function when you add an item.

 

---

 

Okay, that said, I strongly urge you to stop (if you can) and work on some fundamentals.  Your design has some obvious issues which will make it even harder to progress.  I'm getting a distinct "I'm going to throw a bunch of code at the wall and hope something sticks" feeling.  This isn't a critique of you personally - we've all been there and started off similarly - but your design is, essentially broken.

 

So, some criticism/suggestions:

 

1. You really need to ditch your ajaxpage() function.  Simply put, it doesn't do what you want, and the workaround I showed you is really just a hack.  You also don't seem to understand how it works completely, despite the times I've explained it.  That could be due to a language barrier issue, but I'm sensing there's more to it than that.

 

There's no reason not to learn some JavaScript and jQuery for real, especially if you're writing an e-commerce app.  Throwing a bunch of requests through a 3rd party ajax function you don't fully understand is only going to end in heartbreak.  JavaScript is a requirement if you want to be a professional developer.  Might as well start learning it.  You could solve all of your ajax problems with a couple $.get() calls.

 

2. Your cart.php script has some potential.  The problem there is that I don't think you have a clear idea of how your cart should actually work.  You need to sit down and figure out when you want to send output back to the browser and when it's okay to simply update the internal values.  Don't be afraid to write new code.  I keep getting the feeling you're stuck on a few actions that the cart should perform, and you're just shoving code into them hoping it will work.  Make it work, first, and then trim where necessary.

 

And, really, you should focus on getting the PHP script working perfectly before trying to add JavaScript on top of it.  You're trying to do it all at once, which is causing confusion.  The back end is the most important part.  Your HTML and JavaScript should simply be viewed as a skin you place upon your PHP script.

 

3. A small thing, compared to the others, but learn Separation of Concerns (SoC), Don't Repeat Yourself (DRY), and Unobtrusive JavaScript.  It will make your life so much simpler in the long run if you understand and follow those principles.  It will lead to much better written code.  Google each of those terms, and really try to understand them.

 

I'm not saying this to discourage you, only to point out that you're going down the wrong road with your design.  Take a deep breath, forget about the ajax component, and work on getting the PHP part of it as good as it can be.

Share this post


Link to post
Share on other sites

ok... I understand what your are saying (or at least I try to)..

 

But everywhere I look for opening a link in a targeted div I end up reading "the only way to do this is by using ajax and javascript"

 

The only thing I managed to do is to use something like

<head>
<?php
$page=$_GET['page'].'.php';
if (strpos($page,'/')!==false || !file_exists("$page")) $page='header.php';
?>
</head>

<body>
<div id="header">
<?php include($page); ?>
</div>
<div id="content">
<?php include('arxiki.php'); ?>
</div>
<div id="footer">
<?php include('footer.php'); ?>
</div>

or similar code (without strpos) in my index.php and

<a href="index.php?page=body" class="difflink">Body</a>

at my body.php but the only thing I managed is to open the link inside the current div.

 

My <div id=header> is position: fixed and includes the nav bar as well... So I need the link to be opened inside my <div id=content>.

I am not an expert on programming, maybe not even above average but at least I am searching for it... But all I find is pointing to ajax and javascript....

 

Now is there something to add to the above code so it opens me the link in any specific targeted div???

 

Thank you for your patient and help.

Share this post


Link to post
Share on other sites

You don't need ajax for this.  You've just shown that you can dynamically include data based on an incoming $_GET value.  What you want to do with your cart is very similar to that.  You want to update your HTML based on the request sent into your system.  At the top of your script, you need to capture the request data, see what it contains, and then branch off into the right function(s).  Again, you've already done this above with your includes being based on incoming GET data.

 

I'm getting the feeling you don't actually understand what ajax is, so here's a quick explanation.  Ajax is short for Asynchronous JavaScript and XML.  It's asynchronous since it can send HTTP requests to the server, and capture the responses to those requests, without requiring the browser to refresh the current page.  JavaScript is the language which does this communication and captures the responses.  XML is a bit of a misnomer.  Originally, responses were to be little bits of XML.  Today, XML isn't used much as a response.  Instead, JavaScript Object Notation - JSON - is the response format of choice.  That said, anything returned by the server is a response, which is why your raw HTML was suddenly showing up when you didn't want it to.

 

So, in a nutshell, ajax is a bit of JavaScript code that sends requests to some server side code (in our case, PHP).  The PHP code executes and sends back a response.  The JavaScript code then captures that response for later use (such as sticking it all in a <div>, which is what you want to do).  It's not all that different from the normal HTTP request cycle.  The biggest differences are that JavaScript makes the requests for you, and since it captures the response, you can dynamically place that data anywhere on the screen without a refresh.

 

So, for absolute clarity, all ajax will do for you is stop the page from completely refreshing when a HTTP request is made.  The same, boring GET and POST requests are sent to the server when you use ajax.  The process, behind the scenes is exactly the same.  This is why I want you to focus on simply getting the PHP to work.  If you can get your script to successfully add/remove/edit your cart without ajax, it won't take much to add ajax to the process.

 

I think I've said about all I can, short of actually writing your code for you.  Focus on your PHP cart.  Remember: you can include your cart based on a request value, then run the appropriate cart function with that value, and, finally, display the results.

Share this post


Link to post
Share on other sites

ooook...

 

Now I am using this index.php:

<body>
<div id="header">
<?php include('header.php'); ?>
</div>
<div id="content">
<?php
$page = $_GET['page'];
if (!empty($page)) {
	$page .= '.php';
	include($page);
}
else {
	include('arxiki.php');
}
?>
</div>
<div id="footer">
<?php include('footer.php'); ?>
</div>
</body>

 

and:

<a href="index.php?page=body" class="difflink">Body</a>

 

to open a link ehere I need it to open.

 

Problem 1. What I am thinking to do in order to add items to my cart without changing where I am (sending me to the cart.php) is to include each one of my items in a <form> and use a <submit button> to add the item.

Is there a better solution? (IF what I said is going to work)

 

Problem2. When I click the link the page is reloading (logig since I have

<a href="index..."

) and it just not look nice... I need only the content to be refreshed.

How can this be done?

 

Thank you

Share this post


Link to post
Share on other sites

Problem 1: That will work.  In fact that's how some existing sites do it.

 

Problem 2: :facepalm:

 

You really need to read (or re-read) what I wrote:

 

So, in a nutshell, ajax is a bit of JavaScript code that sends requests to some server side code (in our case, PHP).  The PHP code executes and sends back a response.  The JavaScript code then captures that response for later use (such as sticking it all in a <div>, which is what you want to do).  It's not all that different from the normal HTTP request cycle.  The biggest differences are that JavaScript makes the requests for you, and since it captures the response, you can dynamically place that data anywhere on the screen without a refresh.

 

So, for absolute clarity, all ajax will do for you is stop the page from completely refreshing when a HTTP request is made.  The same, boring GET and POST requests are sent to the server when you use ajax.  The process, behind the scenes is exactly the same.  This is why I want you to focus on simply getting the PHP to work.  If you can get your script to successfully add/remove/edit your cart without ajax, it won't take much to add ajax to the process.

 

I really can't make it more clear than that.

 

---

 

Changing gears a bit, you should never blindly accept incoming data of any kind.  This is especially true if you're using that data to decide which page to include.  You should have an array of allowed file names and you should check your $_GET['page'] actually contains one of those names.

Share this post


Link to post
Share on other sites
Problem 1: That will work.  In fact that's how some existing sites do it.

I will go with it then.

 

Changing gears a bit, you should never blindly accept incoming data of any kind.  This is especially true if you're using that data to decide which page to include().  You should have an array of allowed file names and you should check your $_GET['page'] actually contains one of those names.

This I will do.

 

Problem 2:  :facepalm:

Now as for AJAX... As I said, "my" previous script was not mine so... I could use some help here...! I have no idea regarding AJAX.

Share this post


Link to post
Share on other sites

For ajax code examples, you should look at the two jQuery links I gave you earlier in the thread.  jQuery is nice in that all of the complicated ajax code is hidden from view, within the depths of the framework.  This will allow you to really see what ajax does without getting interfered with by technical details.  You won't find easier code examples on ajax.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.