1

I need to have the submitted fruit names to be printed after "Sadrzaj korpe:", i.e "apple, orange, banana".

My code only prints one fruit.

<form>  
    <label for="voce">Voce:</label>
    <input type="text" name="voce">
    <input type="submit" name="submit" value="Ubaci voce u korpu"><br>

 </form>  
    <?php
 if ($_GET["submit"]){
        if ($_GET["voce"]){ 
                echo "Sadrzaj korpe je ".$_GET['voce'];
            }
        }           
Andrew Breksa
  • 308
  • 2
  • 19
dukaPA
  • 19
  • 1
  • 6
  • You have to store somewhere those values(like in database) and later print them all on submit. – Vucko Nov 26 '14 at 07:30
  • you wrote `if -> get submit -> get voce -> print out value of voce `... there is no hinting of saving you value in a db or array. if you want it to be saved, use then an array to save it in. – Dorvalla Nov 26 '14 at 07:30
  • Do you want to submit multiple fruits in one GET request? – Andrew Breksa Nov 26 '14 at 07:37
  • @dukaPA You have a couple of options, do you want a fixed number of fruit that can be submitted, or a dynamic number? – Andrew Breksa Nov 26 '14 at 07:42
  • dynamic @AndrewBreksa – dukaPA Nov 26 '14 at 07:48
  • @dukaPA This post will tell you how to handle an array of POST data on the backend: http://stackoverflow.com/questions/15819920/best-way-to-handle-dynamic-amount-of-form-fields-in-php This will show you how to make dynamic forms with jQuery: http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery – Andrew Breksa Nov 26 '14 at 07:49

3 Answers3

2

Regarding the backend (From here):

input type="text" name="hoursWorked[]" /> will internally convert to an array under $_POST['hoursWorked'].

That means you can do something like this with the form:

<form method="post" action="collect_vals.php">
<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
</div>
</form>

Then, in PHP:

<?php
if(isset($_POST["mytext"])){       
    $capture_field_vals ="";
    foreach($_POST["mytext"] as $key => $text_field){
        $capture_field_vals .= $text_field .", "; //Here is where the values are concated to $capture_field_vals
    }
    echo $capture_field_vals;
}
?>

Regarding the dynamic form (From here):

The JavaScript:

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

The HTML form itself:

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>
Community
  • 1
  • 1
Andrew Breksa
  • 308
  • 2
  • 19
-1

Firstly, you can use isset() to check for $_GET, $_POST etc values.

Secondly, if I understand you correctly, you want to submit several fruit one after another and display them all in real-time?

For that you should use Ajax.

Otherwise, if your goal is only to dynamically display data added on the client then use client side with a library like AngularJS or any other ones that use two-way data binding.

You don't need server side if just showing data changed data set to a user? You can send data to server when user has finished adding his list of fruits.

trainoasis
  • 6,419
  • 12
  • 51
  • 82
-1

Refer this link

  if (isset($_POST["submit"])) {
        echo "Yes";     
  }else{  
       echo "N0";
  }
Flexo
  • 87,323
  • 22
  • 191
  • 272
jay.jivani
  • 1,560
  • 1
  • 16
  • 33