Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form a Javascript array and submit to php

    I am trying to form a form that has one field that will be an array because there are multiple fields with the same name and id that will be generated from it.
    PHP Code:
    <form class="responseForm"  action="javascript:parseResponse123<?php echo $id?>()" id="responseForm<?php echo $id?>"> <input name="hiddenField5<?php echo $id?>" id="hiddenField5<?php echo $id?>" type="hidden" value="<?php echo $id?>" />
    <input name="hiddenField4<?php echo $id?>" type="hidden" id="hiddenField4<?php echo $id?>" value="<?php echo $qty?>" />

    <font color="red"><?php echo $val?></font><br />
    <?php
    $i
    =1;
    while(
    $i<=$qty)
      {
    ?>
    Participant <?php echo $i?>:<input name="hiddenField2<?php echo $id?>[]" id="hiddenField2<?php echo $id?>[]" type="text" /><br />
    <?php  $i++;
      }
      
    ?>
     <input name="submit" type="submit" value="Submit" />
    </form>
    Then I want to use javascript to form the array for the hiddenfield2 parameter and use this code to submit to insert.php

    Code:
    <script type="text/javascript">
    function parseResponse123<?php echo $id; ?> () 
    {
    	var hiddenField2<?php echo $id; ?> = $("#hiddenField2<?php echo $id; ?>");
    	var hiddenField4<?php echo $id; ?> = $("#hiddenField4<?php echo $id; ?>");
    	var hiddenField5<?php echo $id; ?> = $("#hiddenField5<?php echo $id; ?>");
    	var hiddenField1<?php echo $id; ?> = $("#hiddenField1<?php echo $id; ?>");
    
    	var url = "insert.php";
    	
    		$.post(url,{  hiddenField2[]: hiddenField2<?php echo $id; ?>.val(),hiddenField: hiddenField5<?php echo $id; ?>.val(), hiddenField1:hiddenField1<?php echo $id; ?>.val(), hiddenField4:hiddenField4<?php echo $id; ?>.val()  } , 
    		function(data) {
    	
    		});
    		setTimeout(function() {
    
    		$.ajax({
    		type:"POST",
    		data:"getNews=true",
    		success: function(r){
    			$("#newsContent").html(r);
    			
    		},
    		error: function(){
    alert($(".hiddenField2<?php echo $id; ?>").val());		
    	$("#error").text($(".hiddenField2<?php echo $id; ?>")).fadeIn(300)
    		}
    	})
    
    
    	},200);
    }
    
    </script> 
    <?php
    then decode the array using php(I have not gotten this far as I haven't been able to submit any formed array)


    any help from anyone?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,695
    Thanks
    25
    Thanked 657 Times in 656 Posts
    Without looking at your code, this is how I send an array in a form using the hidden input tag.

    Turn the array into a string using join():
    BigString = MyArray.join(); <= this is javascript

    Put that in the hidden field.
    To get an array back in the php use the explode:

    $MyArray = explode(",", $string); <= This is php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •