Link to home
Create AccountLog in
Avatar of sabecs
sabecs

asked on

jQuery - can I include an external PHP file via jQuery?

Hi,
is it possible to include a PHP file using jQuery from within a javascript function?

I want to include an external PHP file called include_credit_options.php if payment_option of credit is chosen?


   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="ScriptLibrary/jquery-1.4.3.min.js"></script>
</head>


<body>


<script language="javascript" type="text/javascript">         
		window.onload = function() {
			show_payment_option();
		}

function show_payment_option(which_option){ 
		if (typeof which_option == "undefined") {
    	var which_option = "<?php echo $_SESSION['payment_option'] ?>";
  		}
		 if (which_option == 'cheque'){
		   document.getElementById('cheque_option_details').style.display = 'block'; 
		   document.getElementById('direct_option_details').style.display = 'none';
		}else if (which_option == 'direct'){
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'block'; 
		}else if (which_option == 'credit'){
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		 //code here to include external php file in code?????  
		}else {
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		}

} 

</script> 
<select name="order_details[payment_option]" class="dropdownmenu" style="width:280px" id="payment_option" onChange="show_payment_option(payment_option.value)">
<option value = "">Select Payment Option</option>
<option value="cheque" <?php if (!(strcmp("cheque", $_SESSION['payment_option']))) echo "SELECTED"; ?>>Cheque - must clear before goods dispatched</option>
<option value="direct" <?php if (!(strcmp("direct", $_SESSION['payment_option']))) echo "SELECTED"; ?>>Direct Deposit into our Bank Account </option>
<option value="credit" <?php if (!(strcmp("credit", $_SESSION['payment_option']))) echo "SELECTED"; ?>>Credit Card Payment</option>
</select>


<div id="direct_option_details">
<div class="inputname" style="height:140px"><strong>Our Direct Deposit Details</strong>
</div>
<div class="inputfield" style="height:140px">
<strong>Bank: </strong>bank_name</br>
<strong>Account Name: </strong>account_name</br>
<strong>BSB: </strong>bsb</br>
<strong>Account Number: </strong>account_number</br>
</div>
<div class="inputfield_fullwidth" style="height:100px">
You have elected to pay via direct deposit into our account, our details are shown above.Please ensure you specify your <strong>Name</strong> and <strong>Order ID</strong> when making the transfer. Once we confirm your payment has been successful we will arrange for your order to be dispatched. 
</div>
</div><!-- end of deposit_option_details div  !-->

<div  id="cheque_option_details">
<div class="inputname" style="height:140px">
<strong>Details for Cheque Payments</strong>
</div>
<div class="inputfield" style="height:140px">
<strong>business_name</br></strong>
postal_address1</br>
postal_address2</br>
postal_address3</br>
postal_address4</br>
</div>
<div class="inputfield_fullwidth" style="height:100px">
You have elected to pay by cheque, please make cheque payable to business_name and mail to our postal address below.</br>
Once we confirm you payment has been successful we will arrange for your order to be dispatched. <br />
</div>
</div><!-- end of cheque_option_details div  !-->



</div>


</body>

Open in new window

Avatar of cmalakar
cmalakar
Flag of India image

Avatar of sabecs
sabecs

ASKER

Thanks for your help cmalakar, I had a look at the links and tried to make it work without any luck.

Would you know what code to use to include my external PHP file called include_credit_options.php and would it go into my function show_payment_option?


I would suggest making an AJAX call to your PHP page and then display the response wherever you need to on your page.
I would suggest you to go to above said examples, understand how it works.

It basically uses AJAX, to fill some part of web page, with PHP file response.

You should also fill your page similarly wherever required.
Avatar of sabecs

ASKER

Thanks for your help, I understand what your saying but don't understand how to code it.?
I have my external php file but now how do I include it.

I have tried adding the following to my function show_payment_option, but it does not work?

}else if (which_option == 'credit'){
               document.getElementById('cheque_option_details').style.display = 'none';
               document.getElementById('direct_option_details').style.display = 'none';
             $.ajax({        
                        url: "my_external_filr.php",          
                        type: "GET",                
                        data: data,            
                        cache: false,        
                        success: function (html) {                        
                        //add the content retrieved from ajax and put it in the #content div            
                        $('#content').html(html);                          
                                
                        }            
});

ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sabecs

ASKER

Thanks cmalakar.