hey guys so im trying to insert data from my contact form into database depending on radio button.

as u can c i have 2 radio button both under "spouse ?" and another under "child ?" now when user clicks on "yes" another form pops up for each title respectively(form for spouse and form for child). my issue is inserting the data into database, for the primary form it has its own database table and for spouse and child its own database table. so say the user only clicks on yes for spouse so there is only data for spouse and the primary form or if only they click on yes for child instead of spouse so theres only data for child and the primary form or they have data for all three forms or maybe just data for the primary form. how can i code the if else statement in php for those conditions?

this is a very very simplified version.

<form method="post" action="add_contact.php">
name : <input type="text" name="name">
house address : <input type="text" name="home">
spouse ? <input type="radio" name="spouse" value="Yes">Yes<input type="radio" name="spouse">No
child ? <input type="radio" name="child" value="Yes">Yes<input type="radio" name="child">No
<div>
    name : <input type="text" name="spousename">
    house address : <input type="text" name="spousehome">
</div>
<div>
    name : <input type="text" name="childname">
    house address : <input type="text" name="childhome">
</div>
</form>

this is what i tried, using AND and &&(i read somewhere theres a small difference between the two) but then again i dont think the way i coded is the right or best way.

<?php

$name = mysql_real_escape_string($_POST['name']);
$house = mysql_real_escape_string($_POST['house']);
$spouse = mysql_real_escape_string($_POST['spouse']);
$child = mysql_real_escape_string($_POST['child']);
$childname = mysql_real_escape_string($_POST['childname']);
$childhouse = mysql_real_escape_string($_POST['childhouse']);
$spousename = mysql_real_escape_string($_POST['spousename']);
$spousehouse =mysql_real_escape_string($_POST['spousehouse']);

if($spouse AND $child == "Yes")
{
    require "connection.php";
    $add = mysql_query("INSERT INTO contacts VALUES('$name','$house')");
    $add2 = mysql_query("INSERT INTO dependents VALUES('$spousename','$spousehouse')");
    $add3 = mysql_query("INSERT INTO dependentd VALUES('childname','childhouse')");
}   

if(($spouse == "Yes") AND ($child == "No"))
{
    $addC = mysql_query("INSERT INTO contacts VALUES('$name','$house')");
    $addc = mysql_query("INSERT INTO dependents VALUES('$spousename','$spousehouse')");
}   

and so on

?>

any help/ideas/suggestions is much appreciated.

// always do this:
require "connection.php";
$add = mysql_query("INSERT INTO contacts VALUES('$name','$house')");
$add2=NULL; // so we can do: if ($add2) do stuff
$add3=NULL;
//  wheder we have a child or not, if spouse add it 
if($spouse == "Yes")
    {
    $add2 = mysql_query("INSERT INTO dependents VALUES('$spousename','$spousehouse')");
    }
//  wheder we have a spouse or not, if child add it 
if(($spouse == "Yes") AND ($child == "No"))
    {
    $add3 = mysql_query("INSERT INTO dependentd VALUES('childname','childhouse')");
    }

i modified my coding the way that pzuurveen did it but it does not work. as in nothing happens even when submit button is clicked. the only way it works is when the radio button for "spouse ?" and "child ?" is yes and both have data to be submitted. other than that, other conditions dont work.

oh i fixed it. and its really silly and some of you might wanna throw rocks at me or worse. (sorry :s ) i just had to change some of my html codes. they were conflicting.

i now see that i forgot to remove te spouse in the last part :(

//  wheder we have a spouse or not, if child add it 
if($child == "Yes")
    {
    $add3 = mysql_query("INSERT INTO dependentd VALUES('childname','childhouse')");
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.