tags:

views:

315

answers:

2

Hello everybody. My project is about a school admin I have a page called : createClass.php, where user inserts grade, profile etc. When he press "submit" page called createdClass.php is loading. Inside this page I have all code which insert data into database and also an "if" structure which says : "Class already exists" if in database is another class with same specifications. Also in second page (createdClass.php) i have a small table which shows the place of each student. First time all cells are green (this means that place is free) and if i click one of them appear a popup window which let me to add info about student from that place. If a place is busy the cell will be red (take a look here : http://screencast.com/t/NzM2YzYxNjct). The big problem is that the cell will be red only after refresh the page (the place ask for data from database). If I press refresh appears "class already exists". To test the code i added in a comment all lines which verify and add respectively classroom . I think my problem can be solved with ajax. I'm waiting for an answer. Regards Stefan

A: 

You must redirect the user to another page after inserting data (after POST request, generally speaking).

Change the form in createClass.php to point to the same page and move your database insert code to that file.

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // insert the data here
    header('Location: /createdClass.php');
    exit();
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
...
</form>

(Update) The flow will go like this:

  1. The user is shown a form in createClass.php
  2. When user submits the form, the submission is handled in the same createClass.php, which inserts database row and then shown no page but tells the browser to go to 'createdClass.php'.
  3. Now browser loads createClass.php, which just shows the student table (but makes no database inserts!). Now you can refresh createClass.php as many times as you want without side effects.

Note that the above may not solve all your issues. Without more information (or code) I cannot help more.

jholster
I think your way is good but i didn't understand what exactly i should do. Do i need another page or how? Can you be more precise ? Thanks for time spent to help me. //Stefan
stefanz
Updated my answer.
jholster
Thanks very much. I'll try to do something but i think the best idea is to use Ajax i think. Can you help me with Ajax please?
stefanz
Thanks Yaggo for answer. I did as you told me and now works. I put a opener.reload(); for "studentAdded.php" and now whole "createdClass.php" is refreshing without any problem and also data goes to database, only once. My small problem was with header(), it doesn't want to work but i put echo echo "<script>window.location='createdClass.php'</script>"; Regards Stefan
stefanz
About the header() problem, read the "Remember that..." section: http://fi2.php.net/manual/en/function.header.php
jholster
+1  A: 

To refresh the original window from the popup, use this piece of javascript:

window.opener.location.refresh();
Alex
I did like you told me but the problem is with "refresh()" whole page is refreshing and this means "Class already exists" so isn't a good way. Thanks for answer and if you have another idea feel free to say it. Regards Stefan
stefanz