tags:

views:

56

answers:

2

Hey guys I need help. Have an HTML form that I am submitting using JQUERY ajax instead of updating with a server response, it is updating with the index page html.

The form

  <form   id="publishimages" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
    <input type="submit"  name="publishimages"  value="Publish Selected"  />

</form> 

PHP

if(isset($_POST['publish'])&&$_POST['publish']=="Publish Images"){
//array of sections that may be allowed to have multiple images
$allow_multiple=Array(1 ,7);
$id=filter_input(INPUT_POST,"id",FILTER_SANITIZE_STRING);
$section=filter_input(INPUT_POST,"section",FILTER_SANITIZE_STRING);
//change the string into an array
$id=explode(",", $id);
    //use the array as id
$count=0;
//ensure only 1 pic is published for other sections which do not need gallery
if(!in_array($section, $allow_multiple )){

$query="UPDATE photos SET publish='0' WHERE publish='1' AND section='$section'";

$mysqli->query($query);
echo $mysqli->error;
}
foreach($id as $key=>$value){
    $value=(int)($value);

    if(is_int($value)){

 $sql="UPDATE photos SET publish='1' WHERE id='$value'";

    $result=$mysqli->query($sql);
    echo $mysqli->error;
    if($mysqli->affected_rows>0){$count++;}

    }

}
echo "$count images have been set and will now appear in their respective sections";
}

JQUERY

$('#publishimages').submit(function() {

    //get selected
    var section=$(this).siblings("form").find("#imagesection").val();
    // inside event callbacks 'this' is the DOM element so we first
    // wrap it in a jQuery object and then invoke ajaxSubmit
    var val = [];
    if($(this).find(':checkbox').length >0){
            $(this).find(':checkbox:checked').each(function(i){
            val[i] = $(this).val();
            });
        }
    if($(this).find(':radio').length >0){
            $(this).find(':radio:checked').each(function(i){
            val[i] = $(this).val();
            });
        } 

    $.ajax({
        data:"publish=Publish Images&id="+ val + "&section="+ section,
        type: "POST",
        url: "phpscripts.php",
        success: function(msg){$("#notice").html(msg)
                        }
        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
    });
    return false;
})

This is what the server response looks like, even though it inserts into the database and does wat I want with PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title></title>
     <link type="text/css" rel="stylesheet" href="css/admin.css" />
    <script type="text/javascript" src="../scripts/jquery.js"></script>
    <script type="text/javascript" src="jscripts/jquery.MultiFile.js"></script>
    <script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript" src="jscripts/sitescript.js"></script>
</head>
     <body>
    <div class="logd_div"><span class="logd">You are logged as <span>admin</span></span>&nbsp;|&nbsp:<a href="index.php?page=changepwd">Change Password</a>&nbsp;|&nbsp; <a href="logout.php">Sign Out</a></div>
              <div class="admincontent">

The question is: why is my server response the whole page html and not just the response I am echoing in PHP?

A: 

To confirm -- are you seeing the home page inserted into #notice, or is the browser redirecting to the home page? That is to say, is the default form submit action being canceled or not?

Are you using any sort of web framework like the Zend Framework, Wordpress, PHPCake, etc?

EDIT1: This is an unusual issue. A few followup questions:

  1. Is the entire code of phpscripts.php included in your question? if not, please include a bit more code. Be sure to paste it in between <code> and </code> tags in the StackOverflow.com UI so it's easy for us to read

  2. Try putting the line of code: die('this is a test'); at the top of phpscripts.php. Repeat the process and see if you see the home page, or this is a test. If it's the latter, move the die() function down a few lines and repeat. This will help you diagnose what's wrong. Please post the results if you're still stuck...

Josh
No the home page is being inserted into the #notice div. The updated form appears below the inserted home page
Hones
Are you using any sort of web framework?
Josh
No am not using any frameworks I am coding on my ow
Hones
@Hones: I added a followup question and a troubleshooting suggestion...
Josh
A: 

Thanks you Josh. It turns out in the process of trying to prevent direct access to the phpscripts.php had put this code if(!isset($allowed)){header('Location:index.php');} which was redirecting to the home page.

Thanks for your help!

Hones
Glad I could help. You should accept your own answer by clicking the green check mark, this gets you a badge as well as lets others know your question has been answered so they don't try to answer it anymore.
Josh