Jump to content

use php inside javascript promt box code?


samoht

Recommended Posts

Hello all,

 

I have a question about mixing js and php. I have a checkout section on my site that ends in a confirmation page that has a table of of the ordered items - at this point I want to ask the client if they have a coupon - so I accomplished that by an input button that on click goes to this js function:

 

function javascript_prompt_for_coupon() {

// PROMPTS REQUIRE TWO ARGUMENTS
var message = "Please Enter Your Code";
var promo_code = "6 digit code goes here";

var return_value = prompt(message,promo_code);

// TEST TO SEE IF NULL OR STRING RETURNED
if (return_value != null) {
	// 'OK' SCRIPT GOES HERE
	// This where I want to use php!
} else {
	// 'CANCEL' SCRIPT GOES HERE
}
}

If I can use php here then I would like to update my HTML table with the coupon added. This actually sounds like a job for AJAX

 

Any IDeas??

 

 

Link to comment
https://forums.phpfreaks.com/topic/72025-use-php-inside-javascript-promt-box-code/
Share on other sites

What you are trying to do in your example is definetly an ajax solution. Why not just give an input box for the user to enter a coupon code on the confirmation page if they have one.

 

Once they confirm their order do your checking with php and then update the db.

That is what I'd like to do - but my boss wants everything on one page. He does not want to leave the current page expect to submit the order. So I guess I'll have to go with AJAX - and id I do - I could still use the prompt?

 

My question is then, once I pass the promo_code to the AJAX - search my db for the discount and return that value, how do I write the table structure to update properly?

 

here is a example of my current structure:

<?php
          $numItem  = count($cartContent);
          $subTotal = 0;
          for ($i = 0; $i < $numItem; $i++) {
            extract($cartContent[$i]);
            $subTotal += $PriceSell * $qty;
          ?>
          <tr class="labelcell">
            <td class="content">
              <?php echo "$qty x $Name"; ?>
            </td>
            <td align="right">
              <?php echo displayAmount($PriceSell); ?>
            </td>
            <td align="right">
              <?php echo displayAmount($qty * $PriceSell); ?>
            </td>
          </tr><?php
          }
          ?>
          <tr class="labelcell">
            <td colspan="2" align="right">
              Sub-total
            </td>
            <td align="right">
              <?php echo displayAmount($subTotal); ?>
            </td>
          </tr>
          <tr class="labelcell">
            <td colspan="2" align="right">
              Shipping
            </td>
            <td align="right">
              <?php echo displayAmount($shopConfig['shippingCost']); ?>
            </td>
          </tr>
          <tr class="labelcell">
            <td colspan="2" align="right">
              Total
            </td>
            <td align="right">
              <?php echo displayAmount($shopConfig['shippingCost'] + $subTotal); ?>
            </td>
          </tr>
        </table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.