0

working around with this code, but can not find what is wrong. the following code is as follows:

$_POST from php filling attributes

<button class="final-buy-now-btn" type="submit" 
book="<?php echo $_POST['book'];?>" 
productId="<?php echo $thisProductId?>" 
value="<?php echo $thisProductId ?>" 
<?php if($book === 'christmas'){ echo "country=\"".$thiscountry."\" "; 
echo "city=\"".$thiscity."\">";} ?> 
<?php if($book === 'birthday'){ echo "bday=\"".$bday."\" "; 
echo "month=\"".$month."\">";} ?> 

<a>Buy Now</a>
</button>

after that i am calling final-buy-now-btn which i expect to be making the error, but i couldnt find it:

$('.final-buy-now-btn').click(function(event){
        var book = $(this).attr("book");
        var productId = $(this).attr("productId");                      

        if( book === "christmas" ){
            var country = $(this).attr("country");
            var city = $(this).attr("city");
            $.ajax({
                type: 'POST',               
                url: '../../wp-admin/admin-ajax.php',
                data: { 'action' : 'add_item_meta_data_to_cart_item',
                    'book' : book, 'productId' : productId,
                    'country' : country,'city' : city},
                success: function (res) {
                    if (res) {
                        window.location = 'index.php/cart/';
                    }
                }
            })
        }
        if(book === "birthday"){
            var month = $(this).attr("month");
            var bday = $(this).attr("bday");
            $.ajax({
                type: 'POST',               
                url: '../../wp-admin/admin-ajax.php',
                data: {
                    'action' : 'add_item_meta_data_to_cart_item',
                    'book' : book,'productId' : productId,
                    'month' : month,'bday' : bday},
                success: function (res) {
                    if (res) {                      
                        window.location = 'index.php/cart/';
                    }
                }
            })
        }
    });

then finally from the action that is called, which is my php function, for birthday the values are getting returned but for christmas nothing is getting returned.

 function add_item_meta_data_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
    $item_meta_data = filter_input( INPUT_POST, 'item_meta_data' );
    $item_meta_data = json_decode($item_meta_data);

    echo "<pre>";
    print_r($item_meta_data);
    echo "</pre>";
5
  • You should examine (and post) the html of the buttons (rendered by your first block of php), and examine (and post) the value of res in your ajax success functions. You should use developer tools F12 console tab to check for javascript errors, and the network tab to examine the ajax post and the data you are sending/receiving. Commented Oct 16, 2017 at 16:09
  • @James, i followed the console and on google chrome i can see that all var in my javascript are being filled but when the ajax call all var’s get reset to null Commented Oct 16, 2017 at 16:12
  • Only if when the first if statement is true, on the second if statement i do not get this issue and i get all my posted values to the last php file Commented Oct 16, 2017 at 16:13
  • check the html of christmas button Commented Oct 16, 2017 at 16:18
  • @James do you think it would be a better idea to have a php function print 2 separate buttons depending on if its christmas or birthday rather than having attributes being changed using php? Commented Oct 16, 2017 at 17:48

2 Answers 2

3

The short answer is that you have typo:

if( book === "chritmas" ){

You forgot letter s in JavaScript code

Other things

You should: Using input type hidden instead of attributes or replace attributes to data-city, data-book

Remove attribute <a> in button

Sign up to request clarification or add additional context in comments.

2 Comments

although you are right, and i fixed that in my code, but i still have the same problem
Try using console.log() to find a problem in JavaScript
0

looking at your code, it doesn't appear that you are closing the button tag. try this snippet with the extra > after the month property.

<button class="final-buy-now-btn" type="submit" 
book="<?php echo $_POST['book'];?>" 
productId="<?php echo $thisProductId?>" 
value="<?php echo $thisProductId ?>" 
<?php if($book === 'christmas'){ echo "country=\"".$thiscountry."\" "; 
echo "city=\"".$thiscity."\">";} ?> 
<?php if($book === 'birthday'){ echo "bday=\"".$bday."\" "; 
echo "month=\"".$month."\">";} ?>> 

<a>Buy Now</a>
</button>

if its still not working, run the page through https://validator.w3.org/ to look at the html output and see if they are any other syntax errors.

as rafael mentioned: data-field is the best practice for naming properties in tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.