Jump to content

Recommended Posts

<?php
for($i = 1; $i <= $_POST['numberinparty']; ++$i){

$fname = $_POST[$i .'fname'];
$lname = $_POST[$i .'lname'];
}
?>

 

edit: there is an easier way to do this though...in your form, you can pass arrays to PHP like this:

<input type="text" name="fname[]" />
<input type="text" name="fname[]" />
<input type="text" name="fname[]" />

then in PHP you can loop over it:

foreach($_POST['fname'] as $fname){
  echo $fname;
}

Link to comment
https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744415
Share on other sites

Right now, I have AJAX changing the div containing the firstname / lastname fields based on a drop down menu for the number of guest in party. So i'm not sure if I could do it the way you suggest.

 

Heres the server-side script, well the part that that deals with number of guest

 

 
<?php
if(isset($_GET['numberofguest'])){

$number = $_GET['numberofguest'];

for($i = 1; $i <= $number; ++$i){

echo "
<b><br>Guest #".$i."</b><br />
<li><label for=\"".$i."fname\">First Name<input type=\"text\" name=\"".$i."fname\"><br />
<label for=\"".$i."lname\">Last Name<input type=\"text\" name=\"".$i."lname\"><br /></li><br /><br />";

}
?>

 

That is ran onChange of the dropdown menu containing possible number of guest in party. So yeah, it probably could be done the way you said, but i think right now, its good b/c it works.

 

Well sortof works.. have one more problem

 

<?php 
for($i = 1; $i <= $_POST['numberinparty']; ++$i){

$fname = $_POST[$i .'fname'];
$lname = $_POST[$i .'lname'];
$queryadd = mysql_query("INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$fname', '$lname', '', '', '', '', '', '', '', '', '$_SESSION[huntid]', '', '', '')") or die(mysql_error());

echo "<label><b>Guest ".$i.":</b></label>".$fname." ". $lname."<br /><br>";
}
?>

 

This should be inserting information into a database but it's not. Any ideas? No mysql errors come up.

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744435
Share on other sites

weird...i would double check your db, cus the rows should be showing up. do the queries look correct if you echo them?

 

<?php 
for($i = 1; $i <= $_POST['numberinparty']; ++$i){

$fname = $_POST[$i .'fname'];
$lname = $_POST[$i .'lname'];
$sql = "INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$fname', '$lname', '', '', '', '', '', '', '', '', '$_SESSION[huntid]', '', '', '')";
echo $sql."<br>";
$queryadd = mysql_query($sql) or die(mysql_error());

echo "<label><b>Guest ".$i.":</b></label>".$fname." ". $lname."<br /><br>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744454
Share on other sites

queries look fine.

 

INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'gdggsdg', 'sdfgsdg', '', '', '', '', '', '', '', '', '2353', '', '', '')

Guest 1:gdggsdg sdfgsdg

 

INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'dgfsdg', 'sdfgsdfg', '', '', '', '', '', '', '', '', '2353', '', '', '')

Guest 2:dgfsdg sdfgsdfg

 

INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'sdfgsdg', 'sdgsdfg', '', '', '', '', '', '', '', '', '2353', '', '', '')

Guest 3:sdfgsdg sdgsdfg

 

 

There's a similiar query on the same page

 

 
$query = mysql_query("INSERT INTO hrclienta (cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$_POST[fname]', '$_POST[lname]', '$_POST[address]', '$_POST[city]', '$_POST[zip]', '$_POST[state]', '$_POST[email]', '$_POST[cellphone]', '$_POST[homephone]', '$_POST[fax]', '$_SESSION[huntid]', '', '', 'y')") or die(mysql_error());

 

this one works.. I compared the two, and as far as i can tell, they are identical.

 

Link to comment
https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744457
Share on other sites

do you use phpMyAdmin to manage your database? try pasting the queries into there to see if it reports any errors. but i don't see why they wouldn't work. double check the db you are connecting to is the same db you are looking at. also, how are you confirming "This should be inserting information into a database but it's not"

Link to comment
https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744477
Share on other sites

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.