Jump to content

Recommended Posts

$hasil = array();

for($i=0; $i<=count($hasil); $i++){

    echo '<form action="" method="post">';

    echo "<input type='text' name='nama[$i]' id='' placeholder='masukkan kegiatan anda'><br>";

}

echo '<input type="submit" name="submit" value="masukkan">';

 

echo '</form>';

 

if(isset($_POST["submit"])){

    for($i=0; $i<=count($hasil); $i++){

        if(isset($_POST["nama"][$i])){

        $hasil[$i] = $_POST['nama'][$i];

    }

    }

    var_dump($hasil);

}

   

?>

i have one form and i want to display every data that i have entered, i have tried with my code but only one is showing

Link to comment
https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/
Share on other sites


I would also suggest keeping HTML and PHP separated as much as possible.

Here's an example - have the HTML in the body of the page (Obviously)

<formid="formData"class="checkStyle"action="create.php"method="post"enctype="multipart/form-data"><inputtype="hidden"name="cms[user_id]"value="3">
    <input type="hidden" name="cms[author]" value="<?=Login::full_name()?>">
    <inputtype="hidden"name="action"value="upload"><divclass="file-style"><inputid="file"class="file-input-style"type="file"name="image"><labelfor="file">Select file</label></div><label><selectclass="select-css"name="cms><optionvalue="index"selected>Home</option><optionvalue="blog">Blog</option><optionvalue="about">About</option></select></label><divclass="heading-style"><labelclass="heading_label_style"for="heading">Heading</label><inputclass="enter_input_style"id="heading"type="text"name="cms[heading]"value=""tabindex="1"requiredautofocus></div><divclass="content-style"><labelclass="text_label_style"for="content">Content</label><textareaclass="text_input_style"id="content"name="cms[content]"tabindex="2"></textarea></div><divclass="submit-button"><buttonclass="form-button"type="submit"name="submit"value="enter">submit</button></div></form>

<?php
require_once "../assets/config/config.php";
require_once "../vendor/autoload.php";usePhotoTech\Login;usePhotoTech\CMS;Login::is_login($_SESSION['last_login']);Login::securityCheck();

$save_result =false;if($_SERVER['REQUEST_METHOD']==='POST'){
    $data = $_POST['cms'];try{
        $today = $todayDate =newDateTime('today',newDateTimeZone("America/Detroit"));}catch(Exception $e){}
    $data['date_updated']= $data['date_added']= $today->format("Y-m-d H:i:s");

    $cms =new CMS($data);
    $result = $cms->create();if($result){
        header("Location: gallery.php");exit();}}?><!doctype html><htmllang="en">

PHP at the top of the page (file)

Edited by Strider64

<?php

session_start();

$hasil = array();

for($i=0; $i<=count($hasil); $i++){

echo '<form action="" method="post">';

echo "<input type='text' name='nama[$i]' id='' placeholder='masukkan kegiatan anda'><br>";

echo '<input type="submit" name="submit" value="masukkan">';

echo '</form>';

if(isset($_POST["submit"])){

    $_SESSION['nama'] = $_POST["nama"];

    $_SESSION["hasil"]= $_SESSION['nama'];

    foreach ($_SESSION["hasil"] as $value) {

        echo $value;

    }          

}

}

?>

i have used session and the result is same, only one data can show

with your form inside the

for($i=0; $i<=count($hasil); $i++){

you are overwriting all your inputs, input names each time it runs so you will only see last item in $hasil.

 

session_start();

$hasil = array();

echo '<form action="" method="post">';

	for($i=0; $i<=count($hasil); $i++){
		echo "<input type='text' name='nama[$i]' id='' placeholder='masukkan kegiatan anda'><br>";
	}
    
   echo '<input type="submit" name="submit" value="masukkan">';

echo '</form>';

if(isset($_POST["submit"])){
    $_SESSION['nama'] = $_POST["nama"];
    $_SESSION["hasil"]= $_SESSION['nama'];

    foreach ($_SESSION["hasil"] as $value) {
       echo $value;
   }          

}
 

 

Edited by dodgeitorelse3
typo

your current code will always only output one form field, because count($hasil) is zero, and there's nothing in your code to cause more form fields to be output.

if you are attempting to provide a way of entering a dynamic amount of data, this is typically done by outputting one form field, then using javascript to add a new (set of) field(s) upon pushing a button.

if you want to do this without using javascript, you need to decide the maximum number of fields you want, then use that number to control the looping.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.