3

Is it Possible to call php page under Javascript function? I have an javascript function and I want to call php page if someone press okk

Here is my code so far

function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
  {
  alert("You pressed OK!");
  return true;
  }
else
  {
  alert("You pressed Cancel!");
  }
}

and this Js function i m using here

<td align="center"><input name="Refund" onclick="show_confirm()" type="submit" value="Refund" /></td>

now i want if user press okk then call other php page ...

hakre
  • 193,403
  • 52
  • 435
  • 836
John
  • 175
  • 1
  • 1
  • 12

5 Answers5

8

if you want to redirect to a page:yourphppage.php if the user pressed ok.

 function show_confirm()
 {
      var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
      if (r==true)
        {
        window.location="yourphppage.php";
        return true;
        }
           else
        {
        alert("You pressed Cancel!");
        }
 }
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • :) not able to redirect after using this – John Aug 23 '11 at 06:35
  • i m getin i m using form and action so my action page remains same – John Aug 23 '11 at 06:38
  • i assume you have replaced the : alert("You pressed OK!"); line by the redirect code. and the page really exist. Please update code in the question – Mithun Satheesh Aug 23 '11 at 06:38
  • :)see I am Using Form, Form action is "samepage.php" now in this form i m using Html as in my code ....as above , when i place this code outside the Form it works well, but inside the form "samepage.php" call... actualy i donot want to keep this html outside the form so what can i do ? – John Aug 23 '11 at 06:54
0

If you only want to redirect the page to the "refund" page without using AJAX you can do something along the lines of the following:

(assumes $transaction_id variable is the id number you are looking to refund)

HTML Lines:

    <td align="center">
    <input name="Refund" onclick="show_confirm(<?php echo $transaction_id ?>)" type="submit" value="Refund" />
    </td>

Javascript lines:

    function show_confirm(transaction_id) 
    { 
        var r = confirm("Do You Really want to Refund money! Press ok to Continue "); 
        if (r == true)   
        {   
            alert("You pressed OK!");  
            location.href = "refund.php?transaction_id="+transaction_id; 
            return true;   
        } 
        else   
        {   
            alert("You pressed Cancel!");   
        } 
    } 
DenisPostu
  • 599
  • 3
  • 10
ChrisK
  • 1,392
  • 10
  • 12
0

If you are looking for page direction:

window.location="index.php";

There are plenty other methods to redirect: http://grizzlyweb.com/webmaster/javascripts/redirection.asp

dcai
  • 2,557
  • 3
  • 20
  • 37
0

Place this where you want your redirect to happen:

location.href = "newpage.php";
DenisPostu
  • 599
  • 3
  • 10
0

Checkout
http://www.w3schools.com/xml/xml_http.asp
Then have a look at
http://mootools.net/docs/core/Request/Request
Any of the JS libraries prototype Jquery etc. have a wrapper to make requests and your life easier.
Then just have a button and set its onClick to be the request call to the server and listen for the response.


Some tips you can append data to the request URL to pass your PHP script variables. Checkout
http://www.w3schools.com/tags/ref_urlencode.asp

Tegra Detra
  • 24,551
  • 17
  • 53
  • 78