开发者

php Foreach Loop issue

I have a big form.

This form contains some inputs that can be duplicated by the user.

So they can choose for example how many Claimants have worked on a song and enter some fields for EACH claimant, with a limit of 10 Claimants.

The form will look like this (I will show only 3 now)

echo "<form action=\"\" id=\"submForm\" name=\"submForm\" method=\"get\">";
//1 CLAIMANT
echo "<p><span class=\"labelInput\">".(_t('_cR_name_mandatory'))." </span><input id=\"nameClaimant\" name=\"nameClaimant[]\" value=\"\" type=\"text\" class=\"required commonInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_DOB_mandatory'))." </span><input id=\"DOBClaimant\" name=\"DOBClaimant[]\" value=\"\" type=\"text\" class=\"required littleInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_company'))." </span><input id=\"companyClaimant\" name=\"companyClaimant[]\" val开发者_C百科ue=\"\" type=\"text\" class=\"commonInput\"></p>"; 

//2 CLAIMANT
echo "<p><span class=\"labelInput\">".(_t('_cR_name_mandatory'))." </span><input id=\"nameClaimant\" name=\"nameClaimant[]\" value=\"\" type=\"text\" class=\"required commonInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_DOB_mandatory'))." </span><input id=\"DOBClaimant\" name=\"DOBClaimant[]\" value=\"\" type=\"text\" class=\"required littleInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_company'))." </span><input id=\"companyClaimant\" name=\"companyClaimant[]\" value=\"\" type=\"text\" class=\"commonInput\"></p>"; 

//3 CLAIMANT
echo "<p><span class=\"labelInput\">".(_t('_cR_name_mandatory'))." </span><input id=\"nameClaimant\" name=\"nameClaimant[]\" value=\"\" type=\"text\" class=\"required commonInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_DOB_mandatory'))." </span><input id=\"DOBClaimant\" name=\"DOBClaimant[]\" value=\"\" type=\"text\" class=\"required littleInput\"></p>"; 
echo "<p><span class=\"labelInput\">".(_t('_cR_company'))." </span><input id=\"companyClaimant\" name=\"companyClaimant[]\" value=\"\" type=\"text\" class=\"commonInput\"></p>"; 
echo "<input type=\"submit\" class=\"submBttnClass\" id=\"buttSubm\" name=\"buttSubm\" value=\"".(_t('_cR_submit_button'))."\">"; 
echo "</form>"; 

When the user submits the form I use a jQuery serialize function (I am on jQuery 1.3.2)

echo "<script type=\"text/javascript\">
$(document).ready(function() {
 $('#submForm').validate({   
  submitHandler: function(form) {
  var serialized = $('#submForm').serialize()
  $.get('".$site['url']."modules/yobilab/copyright/classes/DO_submission.php', serialized);
  //alert($('#submForm').serialize());
    window.setTimeout('location.reload()', 8000);
return false;
  form.submit();    
  } 
})

The problem is that the file that processes the Insert in the Database uses a foreach(I will show you my entire foreach with all the inputs that are passed with the serialize function:

if(!empty($_GET['nameClaimant'])){
$nameClaimant = $_GET['nameClaimant'];
$DOBClaimant = $_GET['DOBClaimant'];
$companyClaimant = mysql_real_escape_string($_GET['companyClaimant']);
$emailClaimant = $_GET['emailClaimant'];
$mainPhoneClaimant = $_GET['mainPhoneClaimant'];
$alternatePhoneClaimant = $_GET['alternatePhoneClaimant'];
$mobilePhoneClaimant = $_GET['mobilePhoneClaimant'];
$percentageClaimant = mysql_real_escape_string($_GET['percentageClaimant']);
$addressClaimant = mysql_real_escape_string($_GET['addressClaimant']);
$ZIPClaimant = $_GET['ZIPClaimant'];
$countryClaimant = $_GET['countryClaimant'];
foreach ($nameClaimant as $valueClaimant) {
$insClaim = "INSERT INTO cR_Claimants SET memberID ='".$memberID."', ParentSubmission='".$refNumb."', Name ='".mysql_real_escape_string($valueClaimant)."', DOB='".$DOBClaimant."', Company='".$companyClaimant."', Email='".$emailClaimant."', Address='".$addressClaimant."', ZIPcode ='".$ZIPClaimant."', Country='".$countryClaimant."', MainPhone='".$mainPhoneClaimant."', OtherPhone='".$alternatePhoneClaimant."', MobilePhone='".$mobilePhoneClaimant."', OwnershipPercentage='".$percentageClaimant."'";
$resultinsClaim=mysql_query($insClaim) or die("Error insert Claimants: ".mysql_error());
}
}

The foreach inserts in the database wrong stuff. It shows Array instead of the right value.

I would like to insert the info in the database consequentially with my foreach.

So for Claimant1 will insert its relative info

for Claimant2 will insert its relative info and so on.

It is not working. What am I doing wrong?

I know it is a little boring, but please help me! Lets play with it. You are All great Thank you

I have attached what is doing in the database to let you see

You will see that is also creating empty strings in the database.

Why is that taking also empty values?

Please help me.

php Foreach Loop issue


this happens beacuse in the form name you called it with an array style name="key[]"

So you can't put into the SQL $key because $key at this point is an array.

Also the correct INSERT syntax is INSERT INTO table (cols) VALUES (values)

Addendum

Also I think your form convetions it's pretty bad. I suggest you to go with something like this:

CLAIMANT1
Email: <input name="claimant[0][email]" />
Name: <input name="claimant[0][name]" />


CLAIMANT2
Email: <input name="claimant[1][email]" />
Name: <input name="claimant[1][name]" />

This way all your code is simplified into:

foreach($_POST['caimant'] as $k=>$v) {    //> PSEUDOCODE
 INSERT INTO table VALUES ($v['email'],$v['name']);

}


I suspect that your insert statement should not use

mysql_real_escape_string($valueClaimant)

but

mysql_real_escape_string($nameClaimant)

instead.


Like yes123 said, you have the 'name' fields specified like 'DOBClaimant[]'.

This is the way to handle a form like this where you have more than one entry with the same name. It means it comes out as an array,which is why you get 'Array'... that's what happens when you try to print an array, instead of using the value of the array.

Try a print_r($_GET) before the form. It will show you the structure of your array and then you can learn how to work with it.

You'll need to do the loop differently, probably with a for loop instead of for each.

You can do

$total = count($_GET['DOBClaimant']);

That tells you how many forms were filled out. Then

for($i=0;$i<$total;$i++){
   //do a for loop with $i, which we can use to refer to a place in each array

   $nameClaimant = mysql_real_escape_string($_GET['nameClaimant'][$i]);
   $DOBClaimant = mysql_real_escape_string($_GET['DOBClaimant'][$i]);
   $companyClaimant = mysql_real_escape_string($_GET['companyClaimant'][$i]);

   }

See how this sets up a loop, then you refer to your place in each array with the [$i] dealie.

By the way, you're just mysql_real_escape_string-ing one or two values - you need to do this to EVERY value you put into the an SQL string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜