At the heart of the problem I am attempting to use the value of a HTML dropdown menu to call a php function without reloading the entire page. Given that PHP is a server side language I have incorporated JQuery to handle the onChange event in the hope of only reloading a portion of the page. The PHP function that I want to call (i.e., wp_cart-button_for_product) prints out an add to cart button, but the variables associated with the add to cart button ultimately depend upon the drop down menu. The div tag that I want to reload (i.e., button-reload) is on the current page, but the URL changes depending upon the product. I have experimented with the load and html methods, but have had no luck.
<script type = "text/javascript">
function productchange()
{
var currentrow = $('#productcategory1').val();
$("#jsvar").val(currentrow);
$.ajax({
type: "GET",
url: "http://www.example.com/wp-content/themes/themeX/storevar.php",
data: {'jsvar': currentrow},
complete: function(){
$('#button-reload').load("http://www.example.com/wp-content/themes/themeX/printbutton.php #button-reload > *");
// $('#button-reload').html($('#button-reload',data).html);
// $('#button-reload').load(location.href "#button-reload > *");
}});
return false;
}
</script>
<div id = "button-reload">
<?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
</div>
<form method="get" action="http://www.example.com/wp-content/themes/themeX/storevar.php">
<input type="hidden" name="jsvar" id="jsvar" value="" />
</form>
Storevar.php
<?php
session_start();
$currentrow = $_GET['jsvar'];
echo "Current Row = $currentrow";
$_SESSION['currentrow'] = $currentrow;
echo "Session Currentrow =".$_SESSION['currentrow'];
?>
Printbutton.php
<div id="button-reload">
<?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
</div>
After hours of experimentation:
<script type = "text/javascript">
function productchange()
{
var currentrow = $('#productcategory1').val();
alert(currentrow);
$("#jsvar").val(currentrow);
$.ajax({
type: "GET",
url: "http://example.com/wp-content/themes/themeX/storevar.php",
data: {'jsvar': currentrow},
success: function(response){
alert(response);
var pathname = window.location.href;
alert(pathname);
$('#button-reload').load(pathname + " #button-reload", function(responseTxt, statusTxt, xhr){
if(statusTxt=="success")
alert("Content loaded successfully!");
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusTxt);
});
return false;
}
</script>
function productchange()
{
var currentrow = $('#productcategory1').val();
$('#button-reload').load("http://www.example.com/wp-content/themes/themeX/printbutton.php", {jsvar: currentrow});
}
should be enough.
You were executing an ajax request and then calling load inside the success callback. Check the jquery documentation http://api.jquery.com/load/
EDIT You are missing "session_start();" in your printbutton.php. You can also add your storage code in there so you only need to make one ajax call.
Printbutton.php
<?php
session_start();
$currentrow = $_GET['jsvar'];
echo "Current Row = $currentrow";
$_SESSION['currentrow'] = $currentrow;
?>
<div id="button-reload">
<?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
</div>