Radio Button to Database Problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

digip
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Oct 08, 2012 9:29 pm

Radio Button to Database Problem

Postby digip » Wed Oct 24, 2012 12:19 am

Hi, still a beginner here.
I got everything to work until I got too confident and added a couple radio buttons at the end and it all went to custard. I'm still trying to understand the logic but got totally lost here can anyone help.

Thanks

<?

if ($_POST) {
$msg = "";

foreach($_POST as $k => $v) {
$v = trim($v) ;
$$k =$v ;

if ($v=="") {
$msg = "Please fill in both fields Bec!";
}
}

if ($msg=="") {
$insert = "INSERT INTO Invoices
(company,invoice_number,amount,due_date,type,recurring)
VALUES ('$co','$inv','$amt','$dd','$tp','$rec')";

$link =
mysqli_connect('localhost','digitalp_rec','6Xl0T@ZB,{~f','digitalp_receivables');

if (!mysqli_query($link, $insert)) {
$msg = "Error inserting data";
} Else {
$msg = "Record successfully added";
$co = $inv = $amt = $dd = $tp = "" ;
}

mysqli_close($link);

}

echo "<div class=\"error\">$msg</div>";

} else {
$co = $inv = $amt = $due_date = $tp = $rec = "" ;
}
?>

<FORM METHOD="post" ACTION="<? echo
$_SERVER['PHP_SELF']?>">
<TABLE BORDER="1" CELLPADDING="5">
<TR>
<TH>Company</TH>
<TH>Invoice</TH>
<TH>Amount</TH>
<TH>Due</TH>
<TH>Type</TH>
<TH>Recurring</TH>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="co" VALUE="<?
echo $co ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="inv" VALUE="<?
echo $inv ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="amt" VALUE="<?
echo $amt ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="dd" VALUE="<?
echo $dd ?>" /></TD>
<TD>
<input type="radio" name="type" value= 'tp1'<? echo $pers ?>; />Personal <br>
<input type="radio" name="type" value= 'tp2' <? echo $biz ?>; />Business <br>
</TD>
<?
$pers = 'unchecked';
$biz = 'unchecked';

if (isset($_POST['SUBMIT'])){
$selected_type = $_POST['type'];

if ($selected_type == 'tp1');{
$pers = 'checked';}

else if ($selected_type == 'tp2'); {
$biz = 'checked';
}
}
$tp = ($pers, $biz);
?>
<TD>
<input type="radio" name="type" value= 'rec'<? echo $rec ?>; />Recurring <br>
</TD>




</TR>
</TABLE>
<BR/>
<INPUT TYPE= "SUBMIT" VALUE= "Add Invoice"/>
<INPUT TYPE= "RESET" VALUE="Cancel"/>
</FORM>



</body>

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Radio Button to Database Problem

Postby seandisanti » Wed Oct 24, 2012 8:23 am

you're evaluating $biz and $pers before assigning values to them.

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: Radio Button to Database Problem

Postby MeroD » Wed Oct 24, 2012 11:12 pm

Code: Select all

$tp = $_POST['type']; // 'type' is set radio button value


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest