1

I have files in a folder:

  • view.php
  • control.php

File view.php has two submit buttons that post a value to control.php.

When a user clicks one of submit buttons, it will

  • The post value will be saved in array $questionTemp;
  • Array $questionTemp will inserted into array $question, using array_push($question, $questionTemp)
  • Then redirect a page to view.php

When a user clicks another submit button, it will

  • pop array $question to array $result using $result = array_pop($question);
  • echo the $result element

But I got nothing when try to echo $result (I think there's something when I redirect a page).

How do I this?!?!

view.php

<form action="control.php" method="post">

    No
    <input type="text" name="no" value="" /><br />

    Question
    <textarea name="question" rows="5" cols="20">
    </textarea><br />

    <input type="submit" value="Save" name="save" />
    <input type="submit" value="Echo" name="echo" />
</form>

control.php

<?php
    $question = array();

    if(isset($_POST['save'])){
        $questionTemp = array();
        array_push($questionTemp, $_POST['no']);
        array_push($questionTemp, $_POST['question']);

        array_push($question, $questionTemp);
        echo "<meta http-equiv=\"refresh\" content=\"0; URL=view.php \">";
    }

    if (isset($_POST['echo'])){
        $result = array_pop($question);
        echo $result[0];
        echo $result[1];
    }
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mopr mopr
  • 193
  • 2
  • 4
  • 13
  • 1
    you want a 2 step form ? you could use ajax, or just use session to temporarily save a form step .. – Gntem Mar 16 '13 at 10:49
  • @GeoPhoenix i dont understand the mean of 2 step form.. in my case.. user will insert data to textArea .. then click save.. then insert data to textArea again.. then click save again.. if user finish the job, user will click echo.. if it possible to do this without ajax or session? maybe object oriented? – mopr mopr Mar 16 '13 at 10:58
  • 1
    You should use `jquery` to do that, so you don't need to refresh page and lost your input values. – Amir Mar 16 '13 at 12:17

1 Answers1

1

You may try use a hidden input with a value. In the controller it must be switch or if. Check Stack Overflow question How to access the form's 'name' variable from PHP.

Community
  • 1
  • 1
  • i don't understand how to use hidden input in my case.. can u show me the code? – mopr mopr Mar 16 '13 at 11:00
  • Sorry, i think you use 2 forms. Your isset check will always true, because value in buttoms default setted. –  Mar 16 '13 at 11:05
  • thanks for checking.. i think my problem is my post value is vanish after i redirect a page.. and i don't understand how to use hidden input in my case – mopr mopr Mar 16 '13 at 11:18
  • 1
    Sorry again, I could not see that you have one form. Thought two. If it were two forms, hidden fields would help if both forms indicated one controller. –  Mar 16 '13 at 11:21
  • Also, array $question empty аfter running the script. It must be recorded in db. –  Mar 16 '13 at 11:29
  • 1
    Or you may try use loop in controller, or closure php if use 5.3 –  Mar 16 '13 at 11:38