-3

I am creating a citation form where user will enter the information of a journal(name, title, pages, date etc) and after submitting the form, all input values get saved in php variables and create reference. Now I want to add a function that after submitting the form, user can select which reference style he/she wants. On selecting an option the result should be changed.

  <select name="refer" type="text" class="form-control input" >
     <option value="1">APA</option>
     <option value="2">Harvard</option>
  </select>   

My PHP code is as follows

if(isset($_POST['submit'])){ 
    $fname= $_POST['fname'];
    $mname= $_POST['mname'];
    $lname= $_POST['lname'];
    $title= $_POST['title'];
    $journal= $_POST['journal'];
    $page= $_POST['page'];
    $volume= $_POST['volume'];
    $month= $_POST['month'];
    $year= $_POST['year'];
    $issue= $_POST['issue'];
    $refer= $_POST['refer'];

    switch($refer){
        case '1':   echo "<h4>". ucfirst($lname). ','. ucfirst($fname[0]) .'. (' . $year . '). ' . $title. '. '. $journal. ', '. $volume .'('. $issue .'), '.$page ."</h4>" ;
        break;
        case '2':   echo "<h4>". ucfirst($lname). ','. ucfirst($fname[0]) .'. ' . $year . '. ' . $title. '. '. $journal. ', '. $volume .'('. $issue .'), p.'.$page ."</h4>" ;
        break;
    }
}

But this switch is not working because it is inside isset($_POST['submit']) statement and won't work without submit button click and also I can't put it outside isset($_POST['submit']) statement.

  • Do you want this as a one-time action, so that the user makes this choice in a step after submitting the rest of the data, but _before_ any data gets permanently stored anywhere - or do you want to store the data first, and then enable the user to _change_ this choice later on whenever they want? – 04FS Nov 15 '19 at 11:11
  • How is this question any different from what you asked just yesterday? https://stackoverflow.com/questions/58851425/how-to-keep-php-variables-from-previous-page-after-form-submit Seems to be pretty much the same thing to me, actually. – 04FS Nov 15 '19 at 11:12
  • The ` – RiggsFolly Nov 15 '19 at 11:14
  • 1
    Like highlighted in the answer of your question from yesterday: Sessions are your friend! – CodyKL Nov 15 '19 at 11:15
  • While it is sort of implied, it is not really clear what probelm you are asking for help with here – RiggsFolly Nov 15 '19 at 11:19
  • @RiggsFolly oh yeah i accidently added it . thanks for mentioning – Nayyera Khan Nov 15 '19 at 11:21
  • @04FS maybe the technique is same but for me it was a different task. I am learning PHP. I don't want it one time action. I want to store data first than enable the user to change this choice later on whenever they want. – Nayyera Khan Nov 15 '19 at 11:22
  • 1
    _“I want to store data first than enable the user to change this choice later on whenever they want.”_ - well then this doesn’t belong where you tried to put it in the first place. You need a separate form for that, and it should include some ID that allows you to find the specific record that the user requested to edit (like its database ID, if that is where you are string your data.) And then when that form gets submitted, you perform an UPDATE statement for that record, that sets the new value in the corresponding column. – 04FS Nov 15 '19 at 11:34
  • 1
    Possible duplicate of [How to keep php variables from previous page after form submit](https://stackoverflow.com/questions/58851425/how-to-keep-php-variables-from-previous-page-after-form-submit) – Rockcat Nov 15 '19 at 12:42

1 Answers1

0

<?php
 if(isset($_POST['submit']))
 {
  $name = $_POST['fname'];
  $refer = $_POST['refer'];
  switch($refer){
   case "1":
    echo "<h4>". ($name) ."</h4>";
    break;
   case "2":
    echo "HI 2";
    break;
   default:
    echo "error";
  }
 }
?>
<form method="post" action="">
Name: <input type="text" name="fname" placeholder="name">
<select name="refer" type="text" class="form-control input" >
     <option value="1">APA</option>
     <option value="2">Harvard</option>
  </select>
  <input type="submit" value="submit" name="submit">
</form>