4

When I click on "Register Now" Button, I want to execute 'input.php' in which I have code to insert my data to the database and show a success message. I don't want to leave current page.

<input type="button" id="confirm" value="Register Now" class="button">

<script type="text/javascript">
$(document).ready(function() {
  $("#confirm").click(function() {
    <?php
    include 'input.php';
    ?>
    alert ("data Added successfully.");
  });
});
</script>

My code is giving me "data Added successfully" message but PHP file hasn't executed and no data is added to the database. All necessary data is in session variables.

8
  • are you using any framework ? Commented Jun 25, 2014 at 9:55
  • Nice idea, but you cannot do that. Have a look a using jquery and look up the ajax call. Commented Jun 25, 2014 at 9:55
  • Not Possible Only Ajax is a solution PHP always executed first then js Commented Jun 25, 2014 at 9:58
  • You can send $_post request to input.php using ajax and tell input.php to echo out finish when script is done running and put the message in some div. Commented Jun 25, 2014 at 10:01
  • @A.P. no. not any framework. Commented Jun 25, 2014 at 10:15

6 Answers 6

6

Suggest you try something like the below. You shouldn't be trying to execute PHP inside of a jQuery script. Do an AJAX call and pass the data through and process it in the PHP rather than relying on the session variables. For example:

<script type="text/javascript">
    $(document).ready(function () {
        $("#confirm").click(function () {
            $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: {
                        firstname: "Bob",
                        lastname: "Jones"
                    }
                })
                .done(function (msg) {
                    alert("Data Saved: " + msg);
                });
        });
    });
</script>

Where firstname, lastname would be your normal session data.

You can learn more about the jQuery.ajax() function in the jQuery API Documentation.

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

3 Comments

Thanks, I'am working on it using Ajax now. according to your example i don't have to use Session variables. am i correct? Also as i learned from the url you provided, ".done(function (msg) { " msg means the string my input.php should return. so how can I ask my php to return the errors to this function?
Yes, just return the value from your PHP and it will be ready to use in the 'msg' variable.
OP should not forget about protection of their PHP API and check "referrer" header as minimal requirement, as normal: use session auth tokens
1

To execute a Php script from javascript, you have to use Ajax.

the following code :

$("#confirm").click(function() {
    <?php
    include 'input.php';
    ?>
    alert ("data Added successfully.");
  });

will not work

http://api.jquery.com/jquery.ajax/

Comments

1

You need to use AJAX. Ajax is the concept of calling php files from javascript from inside the page. You then get the php page output in a variable and you can choose wether you will display it or not. An example of this technology is the show more posts of Facebook. This can be easily done with jQuery.

$.post( PHP_FILE, { field1: 'value', field2: 'value'}).done(function( data ) 
{alert("this function will be run when the request is over and the variable data 
will have the output : " + data);});

Comments

0

you can do this by ajax post method..

 $.ready(function(){
 $("#confirm").click(function() {
 $.ajax({
 type: "POST",
 url: "give url to the input.php file ",
 data:,
 success:function(data)
 {
  alert('data');// data is the return value from input.php
  }
  });
  });

});

Comments

-1

Try this:

<input type="button" id="confirm" value="Register Now" class="button">    
<script type="text/javascript">
$(document).ready(function() {
  $("#confirm").click(function() {
    $.get('input.php').
    success(function(){
       alert ("data Added successfully.");
    });
  });
});
</script>

1 Comment

Can you try adding the cdn link of JQuery and try. <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>.
-1

I'm not quite sure what you've tried to do there. Anyway here's a snippet of js that should do for you (I'm pretty sure in the new jQuery release there's a better way to do this though):

$.ajax({ 
    url: "input.php",
    success: 
        function(data)
        {
            // here, for example, you load the data received from input.php into
            // an html element with id #content and show an alert message
            $("#content").html(data);
            alert("Success")
        }
});

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.