0

Hi my program should be able to draw the SQL count from a certain table, and generate the number of textbox for the counted number.

What i actually want is, based on the alphabet valued button I click, show the number of textboxes for the items that starts with the alphabet button clicked.

<button type="submit" name="sort" id="S" value = "S" onclick="<?php $alphabet='S'; ?>">S</button>

<?php $query = "select count(*) from product where productname like '$alphabet%' ";
                $result = mysqli_query($dbconn,$query);
                while($row= mysqli_fetch_array($result)) {
                $count=$row['count(*)'];                    
                }
for($counter = 0; $counter < $count; $counter++){ //Create text boxes and add to cart buttons
                echo "<br>";
                echo "<br>";                  
                echo "<label>Product ID: </label><input readonly type='text' name='productname' id=''><br>";
                echo "<label>Product Name: </label><input readonly type='text' name='opening' id='' >"
                    ."<button type='button' class='deliver' id='' onclick=''>Add To Cart</button><br>"
                    ."<button type='submit' class='deliver' id='viewcart' onclick='window.location.href='BACart.php';'>View Cart</button><br>";                                      
                echo "<label>Quantity: </label>";
                echo "<button type='button' class='quantityaddsub' id='sub' onclick='quantitysub($counter)'>-</button>";
                echo "<input type='text' class='quantity' name='' id='quantity".$counter."' value=0>"; 
                echo "<button type='button' class='quantityaddsub' id='add' onclick='quantityadd($counter)'>+</button><br> ";
            }?>

I find it really tough to do so. Is my method correct, or is there a easier way to do it? Or do i have to use AJAX to call our php function in javascript as I have read elsewhere..?

Jongware
  • 22,200
  • 8
  • 54
  • 100
mctjl_1997
  • 163
  • 1
  • 3
  • 17
  • 3
    Short answer: You have to use AJAX. – Dan May 02 '16 at 14:54
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Machavity May 02 '16 at 18:34

4 Answers4

2

You have to use AJAX because the PHP code is executed on the server, while javascript is executed by the browser.

The browser communicates with the server through requests and responses (or websockets, but that's a different technique) and the technique that facilitates this type of communication is called AJAX (as you also mentioned)

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • Don't you think you should atleast give him some links to learn from? Or, rather show him an example... ? – Ikari May 02 '16 at 15:06
  • He already knew about ajax, I think he just needed a confirmation. However, I expanded my answer a little bit – Tudor Constantin May 02 '16 at 15:17
  • Actually i dont know about ajax, i've just seen some people on stackoverflow talk about ajax, and im just wondering if using ajax is the only way to solve my problem. But thanks man :) I'll look into it – mctjl_1997 May 03 '16 at 01:21
  • I meant that you knew about the existence of AJAX and you grasped the concept properly, you just needed a confirmation that it's just what worked here. – Tudor Constantin May 03 '16 at 05:37
  • 1
    Thanks man! I managed to solve my problem through AJAX, thanks for letting me know more about AJAX. You can view my follow up answer to this question :) – mctjl_1997 May 06 '16 at 18:08
0

Only for complete the answer of Marcus.

If you use jQuery, can use $.ajax to execute a ajax code, the documentation for the funciona is here.

Otherwise, you can use pure javascript to do this request, in w3schools, we found this example

0

The answer

No, you cannot run PHP code on a Javascript Event Listener

Why?

The PHP code is ran before that Javascript code is ever even in the DOM (rather, before the server even sends HTML to the client's browser). Therefor, you can put PHP code in to get the output of said code. However, once the DOM is presented to the browser, the PHP code is 100% gone and out of the equation (only leaving things that have been deliberately output, like by echo for example). The end-user (and their browser) will never see your PHP code.

Example

var test = "<?php echo "HELLO!"; ?>";

In the DOM/Source, this Javascript code would simply read as:

var test = "HELLO!";

Possible Solution(s)

As mentioned, use AJAX to hit a PHP script on-demand.

Nate I
  • 946
  • 4
  • 10
-1

I managed to solve this problem! All i did was get the code from this site http://www.w3schools.com/php/php_ajax_database.asp and after reading through the tutorial, i understood somehow and changed some of the code to fit my buttons instead of dropdown list and changed the sql statements. Learning ajax isn't as hard as i thought, all thanks to W3's open source code that is online. Even thought i wanted to show textboxes but html tables seems fine so i'll make do with that!

Vote this answer up so that whoever wants to do what I'm doing can learn from this! Thanks to all who answered my question previously :)

mctjl_1997
  • 163
  • 1
  • 3
  • 17