Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2013, 09:09 PM   PM User | #1
searls03
New Coder

 
Join Date: Jun 2011
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
searls03 is an unknown quantity at this point
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?
searls03 is offline   Reply With Quote
Old 02-20-2013, 02:34 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
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
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:45 PM.


Advertisement
Log in to turn off these ads.