subendi Posted December 11, 2022 Share Posted December 11, 2022 $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 Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/ Share on other sites More sharing options...
requinix Posted December 11, 2022 Share Posted December 11, 2022 PHP will not remember $hasil from the last time you used the form. You have to store the data somewhere and then put it into $hasil before you display the form. Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603430 Share on other sites More sharing options...
subendi Posted December 11, 2022 Author Share Posted December 11, 2022 can you provide an example? Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603435 Share on other sites More sharing options...
requinix Posted December 11, 2022 Share Posted December 11, 2022 I could spend my time trying to do that, yes. Or I could suggest that you look into how to use databases, or perhaps just sessions, in PHP - you should be able to find tons of examples about those. Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603436 Share on other sites More sharing options...
Strider64 Posted December 11, 2022 Share Posted December 11, 2022 (edited) 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 December 11, 2022 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603437 Share on other sites More sharing options...
subendi Posted December 11, 2022 Author Share Posted December 11, 2022 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603441 Share on other sites More sharing options...
dodgeitorelse3 Posted December 11, 2022 Share Posted December 11, 2022 (edited) 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 December 11, 2022 by dodgeitorelse3 typo Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603443 Share on other sites More sharing options...
mac_gyver Posted December 11, 2022 Share Posted December 11, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315636-insert-one-form-into-array/#findComment-1603444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.