0

OK , here is my issue;

My issue is that when i callback my php file with ajax it calls all the content of my file

here is my code

my source.php file which including my view.php file

<?php
    if(isset($_POST['test'])){
       echo "OK";
    }
  include_once "view.php";
?>

my view.php file

<!DOCTYPE html>

<html>

<head>
  <title>Hello!</title>
</head>

<body>


<h1>Form</h1>

<form action="" method="post" id="form">
    <input type="text" name="test" id="test" /><br>
    <input type="submit" id="sub" name="sub" />
</form>

<div id="result"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
    $(document).ready(function(){
       $("#sub").click(function(){


        $.ajax({
           type: "POST",
           data: $('#form').serialize(),
           success: function(slider_data){
             $("#result").text(slider_data);
           }
        });

          return false;
       });
    });
</script>


</body>
</html>

Now when i submit my form my #result here is

OK<!DOCTYPE html>

 <html>

 <head>
 <title>Hello!</title>
 </head>

 <body>


 <h1>Form</h1>

 <form action="" method="post" id="form">
 <input type="text" name="test" id="test" /><br>
 <input type="submit" id="sub" name="sub" />
 </form>

 <div id="result"></div>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

 <script>
 $(document).ready(function(){
 $("#sub").click(function(){


 $.ajax({
 type: "POST",
 data: $('#form').serialize(),
 success: function(slider_data){
 $("#result").text(slider_data);
 }
 });

 return false;
 });
 });
 </script>


 </body>
 </html>

i want to show only the word "OK" or whatever i want and prevet sending all data and included file

1
  • because you have include in source.php Commented May 31, 2014 at 8:25

3 Answers 3

1

because you have include in source.php that's why you are getting view.php again.

change your code like this

source.php

<?php
    if(isset($_POST['test'])){
       echo "OK";
    }
?>

<script> in view.php will be like this

<script>
    $(document).ready(function(){
       $("#sub").click(function(event){
        event.prevantDefault();

        $.ajax({
           url:"source.php" // url to source.php 
           type: "POST",
           data: $('#form').serialize(),
           success: function(slider_data){
             $("#result").text(slider_data);
           }
        });

          return false;
       });
    });
</script>
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks but as i said in previous comment i want to remove that refresh confirm :)
try removing event.preventDefault();
if i removed it , it will show me again my view file data :)
have you removed include from source.php, and added url: in your $.ajax ?
But why i remove my included file !! i want to display my view file in my source file and wantr the default url to be the same page (source.php) without writing it ^_^
|
1

Add e.prevantDefault(); in order to prevent form submission. And also add a die() after your echo 'Ok'; to stop the execution .So try like this

 $("#sub").click(function(e){
        e.prevantDefault();
        $.ajax({
           type: "POST",
           data: $('#form').serialize(),
           success: function(slider_data){
             $("#result").text(slider_data);
           }
        });
  });

if(isset($_POST['test'])){
   echo "OK";
   die();
}
include_once "view.php";

1 Comment

Thanks , works perfectly but still there an issue, now when i submit my form , when i refresh the page it shows me that alert message to refresh the page or not which i disabled it in my previous code with return false , but when i added it to your code ,it doesn't work anymore
0

You can check if the page was requested with AJAX, and only include the view if it's not.

if(isset($_POST['test'])){
   echo "OK";
}

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ) !== 'xmlhttprequest') {
    include_once 'view.php'; 
}

The second if checks if the page was not requested with AJAX.

Echoing "ok" before the include_once would break the site, since it outputs content before the <DOCTYPE html> tag.

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.