2 Replies - 1873 Views - Last Post: 01 April 2014 - 08:54 AM

#1 Wolverine89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 14-January 14

AJAX function to PHP function and parameters

Posted 01 April 2014 - 04:31 AM

Hi guys,

I code a button, when clicked on this button an javascript function is started and sends an parameter to a php file. This php file must also execute so you are send to another location.

HTML:
echo "<td><input id='submit' type='button' name='addToCart' value='Add' onclick='addToCart($book_id);'></td>";


AJAX:
<script type="text/javascript">		
	function addToCart(book_id) {
		var id = book_id;
		var current_page = "<?php echo $current_page ?>";
		$.ajax({
			  type: 'post',
			  url: '/workspace/webshop/model/Shopping_cart.php',
		  	data: {book_id:id, cpage:currentpage}
		});
	}
</script>



PHP FILE:
<?php
session_start();
 
// get the product id
$id = isset($_POST['book_id']);
$current_page = isset($_POST['current_page']);
 
/* 
 * check if the 'cart' session array was created
 * if it is NOT, create the 'cart' session array
 */
if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}
 
// check if the item is in the array, if it is, do not add
if(in_array($id, $_SESSION['cart'])){
    // redirect to product list and tell the user it was added to cart
    header('Location: ../index.php?page=Home');
}
 
// else, add the item to the array
else{
    array_push($_SESSION['cart'], $id);
     
    // redirect to product list and tell the user it was added to cart
    header('Location: ../index.php?page=Home');
}
 
?>



But this is not working, i cant even see if something happens when clicked on the button (add) html. I am not located to Home page while this is coded in the if statements who can help me

Is This A Good Question/Topic? 0
  • +

Replies To: AJAX function to PHP function and parameters

#2 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: AJAX function to PHP function and parameters

Posted 01 April 2014 - 07:28 AM

Quote

// get the product id
$id = isset($_POST['book_id']);
$current_page = isset($_POST['current_page']);


Each of these will result in true or false as isset() returns a Boolean value.

Quote

data: {book_id:id, cpage:currentpage}


This does not match

Quote

$id = isset($_POST['book_id']);
$current_page = isset($_POST['current_page']);

This post has been edited by laytonsdad: 01 April 2014 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: AJAX function to PHP function and parameters

Posted 01 April 2014 - 08:54 AM

Also seems pretty silly to use AJAX to post data to a page and then just use a header redirect to change the page. Sort of defeats the purpose of using AJAX in the first place.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1