0

My coding isn't working. Please anyone could tell me why? Here's the code:

AJAX code:

function sale(sale_code,sale_quantity) {
        if (sale_code.length == 0 || sale_quantity == 0) {
            alert("Product's code & quantity are required!");
            return;
        } else {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("show").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET", "sl.php?sdoc_no=<?= $sdoc_no ?>&sdoc_date=<?= $sdoc_date ?>&sale_code=" + sale_code + "&sale_quantity=" + sale_quantity, true);
            xmlhttp.send();

        }
    }

HTML & PHP:

<?php $qitem = mysqli_query($conn, "SELECT * FROM stocks WHERE stock_type = 'product'");
while($ritem = mysqli_fetch_array($qitem)){ ?>

<div class="col-md-4 col-sm-6 text-center">
    <button class="btn btn-info btn-block" type="button" onclick="sale(<?= $ritem['stock_code'] ?>, 1)"><?= $ritem["stock_name"] ?><br />
        <small>RM <?= number_format($ritem["stock_sprice"], 2) ?></small>
    </button>
</div>

<?php } ?>

sl.php file is working and returning data if i'm put it manually. And take note that I'm using bootstrap tab for this button.

8
  • You say "isn't working". What happens? Do you get any error message in the console? Commented Jun 19, 2016 at 8:56
  • Nope. Its all good before i put into bootstrap tab/pills menu. Is it bootstrap problem? Or my code? I checked all my code and it seems no error. Commented Jun 19, 2016 at 8:58
  • What does $ritem['stock_code'] contain? Text or just numbers? Commented Jun 19, 2016 at 8:59
  • Its a string. Maybe barcode int, or may there is varchar Commented Jun 19, 2016 at 9:00
  • Ah.. try this: sale('<?= $ritem['stock_code'] ?>', 1). (Add '-signs around the parameter, which is needed for strings). You should also uri encode your parameters before adding them as querystring, For js: encodeURI and for PHP: urlencode Commented Jun 19, 2016 at 9:04

2 Answers 2

1

HOW COULDN'T I SEE IT...

You have this wrong:

xmlhttp.open("GET", "sl.php?sdoc_no=<?= $sdoc_no ?>&sdoc_date=<?= $sdoc_date ?>&sale_code=" + sale_code + "&sale_quantity=" + sale_quantity, true);

You cannot use PHP in JavaScript... so you have this wrong:

sl.php?sdoc_no=<?= $sdoc_no ?>&sdoc_date=<?= $sdoc_date ?>&sale_code=" + sale_code + "&sale_quantity=" + sale_quantity

This <?= $sdoc_no ?> and <?= $sdoc_date ?> is not valid JS code...

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

10 Comments

I am putting < php instead of <?php becasue this forum text editor doesn't support to much php tag. Btw, it's still not working.
I know about the php tag. What i am gonna say is, the code isnt working.
I edit the php tag. The original file is using the correct tag. Only for this forum i editted it. Do you get it? The problem is, even using the correct tag, still doesn
Look at my new answer
@MrHery - Is your javascript inline js in a .php-file (using <script>-tags) or is it in a separate .js-file?
|
0

You need to have quotes around string parameters in js:

Change:

sale(<?= $ritem['stock_code'] ?>, 1)

to:

sale('<?= $ritem['stock_code'] ?>', 1)

Then you should uri encode your parameters before you use them as url-parameters.

For JS, use encodeURI and for PHP, use urlencode;

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.