-1

I got a html page with 2 forms which are using the same php script..

<?php

    if ($conn) {
        if (isset($_POST['form_student'])) {
            if ($_POST['form_student'] == 'Send') {
                if (!empty($_POST["Ime"]) && !empty($_POST["Prezime"]) && !empty($_POST["BrojIndeksa"]) && !empty($_POST["TipNastave"])) {
                    header('Location: forme.html');
                    echo "<script>alert('Processing data to sql.');</script>";
                } else {
                    echo 'You didnt fill all fields!';
                    echo "<script>alert('You didn't fill all fields!');</script>";
                }
            }
        } 
        if (isset($_POST['form_subject'])) {
            if ($_POST['form_subject'] == 'Send') {
                // same checks just like above with redirecting
                // and displaying alert box
            }
        }
    }

?>

First problem is when I don't fill all fields in, it does work and echo 'You didn't fill al fields!' but doesn't display alert box message, and it only doesn't work when I don't fill all fields. And I'm wonder how can I actually by processing php script, without redirecting to that php script page, show msg box on html page, is it possible with out ajax or jquery, or I should instead using html extension change into php, and do all checks there and avoid processing script into action=""?

Dice Hunter
  • 25
  • 2
  • 4
  • are you getting _You didnt fill all fields!_ ? – NullPoiиteя May 16 '13 at 16:10
  • 1
    `echo` after `header`? :O – Mr. Alien May 16 '13 at 16:11
  • possible duplicate of [PHP: give alert popup then redirect the page](http://stackoverflow.com/questions/11703854/php-give-alert-popup-then-redirect-the-page) – epascarello May 16 '13 at 16:13
  • 2
    this just looks like the entirely wrong way to handle something like this. As Mr. Alien stated, you are trying to echo after a header redirection. You would never see that alert even if it did work. Secondly - come on, javascript alerts? Just write the messages out to the html. – Kai Qing May 16 '13 at 16:13
  • 2
    Hint: The statement `alert('You didn't fill all fields!');` contains **three** apostrophes. – Martijn May 16 '13 at 16:14
  • Yea actually everything works, and I know this is actually stupid what I wrote above with header then echo.. However, how can I on html page where I'm processing the script, display echo ""; so that it doesn't redirect me to script.php and shows the alert there.. – Dice Hunter May 16 '13 at 16:16

1 Answers1

0

That's because of the row ..

echo "alert('You didn't fill all fields!');";

Try this one instead ..

echo "alert(\"You didn't fill all fields!\");";

What you did wrong was that you had an apostrophe in the string and around the string. I don't know how to explain this but I simply made the two quotation marks to not conflict with the php echo.

Update:

Regarding the second question about the redirect and such. Could you explain it further because I don't understand a word?

Magnus
  • 391
  • 1
  • 7
  • 35
  • Thank you for echo "alert(\"You didn't fill all fields!\");"; it works, but only problem left now is how can I on html page where I'm processing the script, display echo ""; so that it doesn't redirect me to script.php and shows the alert there.. – – Dice Hunter May 16 '13 at 16:23
  • When does it redirect you to script.php? Is the code you shown us the code for script.php? – Magnus May 16 '13 at 16:23
  • Well, it redirects when I submit, cuz I'm processing script.php into atribute action="script.php", and ofcourse, it checks what it has to check on this form, and what actually happens it's redirecting me to script.php, which actually contains nothing else then php script which checks forms and as result display alert message box, for which I expect from to be displayed somehow to that same html page. Should I instead use php exe instead of html, and with all html tags together add this checks before form or how it goes.. to achieve effect..? – Dice Hunter May 16 '13 at 16:27
  • I got actuall this idea, is it possible to do page.html?sucess just like you can do with page.php?success – Dice Hunter May 16 '13 at 16:31
  • What you could do is that you could use a button instead of a submit-button. Use jQuery to execute code when the button in pressed and then send the field-values to another page using AJAX. Read more here; http://stackoverflow.com/questions/2866063/submit-form-without-page-reloading and here; http://api.jquery.com/jQuery.ajax/ – Magnus May 16 '13 at 16:31
  • And how about php exe instead of html idea ? Since I'm not allowed to use jquery and ajax :/.. sadness but I can't.. – Dice Hunter May 16 '13 at 16:35
  • And no, I don't think that's possible. I could be wrong but I have no idea how to read GET variables using jQuery or js. – Magnus May 16 '13 at 16:35
  • You must include some jQuery or js to achieve what you want to achieve. – Magnus May 16 '13 at 16:36
  • Mhm.. Alright I think i got an idea... what to do.. thanks for kind a help – Dice Hunter May 16 '13 at 16:37