0

I have html form whose action is a php script. php code basically replaces the file. HTML code:

<form name="input" action="//copy.php" method="POST" onsubmit="return alertbox();">                        

 <input type="hidden" name="path1" value=path to image 1/>
 <input type="hidden" name="path2" value='path to image 2' />
 <input type="submit" name="submit" value="Copy image"/>                

</form> 

Php code:

if isset($_POST['submit']))
   {       
    $image1 = $_POST['path1']; 
    $image2 = $_POST['path2'];
    copy($image1, $image2);
    }       

?>

Now when I click submit, a alert box opens that "file is updated successfully" and when I click ok on it, a blank page load. How can I avoid loading the blank page? I want to stay on the same page after clicking submit with pop up msg.

SOLUTION

As I don't see "Answer your own question" option, I am posting solution here. This link POST form and prevent response, gives you textual answer to the question. While I providing the answer by code.

So basically, it very simpl. Just put

header("HTTP/1.0 204 No Response"); 

in the php file and it will work successfully on all browser, without opening new page. This will avoid use of jquery.

Community
  • 1
  • 1
Kevin
  • 217
  • 6
  • 19

4 Answers4

0

Leave action empty.

<form method="post" action="">

Then check if posted using isset function

if(isset($_POST)){
...
}
Sajad Deyargaroo
  • 1,149
  • 1
  • 7
  • 20
0

This will keep you on the same page after submit button..

<form name="input" action="" method="POST" onsubmit="return alertbox();">

 <input type="hidden" name="path1" value=path to image 1/>
 <input type="hidden" name="path2" value='path to image 2' />
 <input type="submit" name="submit" value="Copy image"/>

</form>

But now, your image(s) may not be loaded. Maybe it will work, maybe not. If not, you will need to resolve functions which may reside in copy.php file. Since we dont know whats in that file, its hard to answer your question correctly, but.. you may try this "blind" shot..

    if(isset($_POST['submit']))
       {       
        //include "//copy.php"; // this file probably contains functions, so lets load functions first IF needed.. 
        $image1 = $_POST['path1']; 
        $image2 = $_POST['path2'];
        copy($image1, $image2);

        } 
Joem
  • 112
  • 6
  • No. That will load a new page from the same URL. – Quentin Dec 26 '13 at 23:55
  • @Quentin My guess is that alertbox() is a pure "Do you want to proceed?" message nothing else. Since PHP action calls copy.php file its kinda obvious. Otherwise, jquery would call that file, not PHP action. But irrelevant. – Joem Dec 27 '13 at 00:03
  • So it can only be done only using jquery, right? or there is way in which I can avoid using jquery (because I don't know it). – Kevin Dec 27 '13 at 14:08
  • I have put solution in my question. thanks for help. – Kevin Dec 27 '13 at 14:46
0

Have you tried changing the type on the input:

 <input type="submit" name="submit" value="Copy image"/>

to

 <input type="button" name="submit" id="submit" value="Copy image"/> 

<input type="button" /> won't submit a form by default (check all browsers to be sure).

<input type="submit"> by default, the tag in which the submit input is, is submitted. If you still want to use this, you will have to override the input submit/button's functionality with an event.preventDefault(). In that case, you need:

$('#submit').click(function (event) {
      event.preventDefault();
     //Do whatever you need to 

   //Submit if needed: document.forms[0].submit();
});

For further details refer this link

Community
  • 1
  • 1
Loser Coder
  • 2,338
  • 8
  • 42
  • 66
0
action="<?php echo $_SERVER['PHP_SELF']; ?>
Asraful Haque
  • 1,109
  • 7
  • 17