Jump to content

Radio button and textbox value to mysql dataase, please help.


phpnewnoob

Recommended Posts

Hey Everybody,

 

I am new to PHP coding and have searched tons of forums and still looking...

 

Please help me with the following:

 

I have a MySQL database. There are several fields in the table, among them are Amount_paid and Amount_returned fields. And there are two radio buttons Amount paid and Amount returned and one textBox where I will be entering an amount. What I need is to assign an entered amount to one of those radio buttons (depending which one is selected) and insert the textBox value into the correct field (amount_paid or amount_returned) of the table.

 

I have all other fields coded ok and they work, but have no idea how to make the above stuff work.

 

The code is attached, please let me know if you need more info. Thanks a lot!

 

EDIT: Sorry, now the file is attached :)

17217_.txt

Link to comment
Share on other sites

Hey Everybody,

 

I am new to PHP coding and have searched tons of forums and still looking...

 

Please help me with the following:

 

I have a MySQL database. There are several fields in the table, among them are Amount_paid and Amount_returned fields. And there are two radio buttons Amount paid and Amount returned and one textBox where I will be entering an amount. What I need is to assign an entered amount to one of those radio buttons (depending which one is selected) and insert the textBox value into the correct field (amount_paid or amount_returned) of the table.

 

I have all other fields coded ok and they work, but have no idea how to make the above stuff work.

 

The code is attached, please let me know if you need more info. Thanks a lot!

 

EDIT: Sorry, now the file is attached :)

 

 

Here is the code so far:

 

<?

 

include ("blocks/db.php");

 

if (isset($_POST['firstname'])) {$firstname = $_POST['firstname']; if ($firstname == '') {unset($firstname);}}

if (isset($_POST['lastname'])) {$lastname = $_POST['lastname']; if ($lastname == '') {unset($lastname);}}

if (isset($_POST['age'])) {$age = $_POST['age']; if ($age == '') {unset($age);}}

 

$myoption = $_POST['gender'];

$sub = $_POST['submitter'];

$bank = $_POST['choosebank'];

 

?>

 

<?

 

if (isset($firstname) && isset($lastname) && isset($age) && ($myoption) && ($sub) && ($bank))

 

{

$result = mysql_query ("INSERT INTO banks (firstname,lastname,age,gender,submitter,choosebank) VALUES ('$firstname','$lastname','$age','$myoption','$sub','$bank')");

 

if ($result == 'true') {echo "<p>Data has been successfully added!</p>";}

 

else

 

{echo "<p>Error occured. Data hasn't been added!</p>";}

 

}

 

else

 

{

echo "<p>Some of the fields are not filled out. Please enter the required data.</p>";

}

 

?>

 

Link to comment
Share on other sites

I have a MySQL database. There are several fields in the table, among them are Amount_paid and Amount_returned fields. And there are two radio buttons Amount paid and Amount returned and one textBox where I will be entering an amount. What I need is to assign an entered amount to one of those radio buttons (depending which one is selected) and insert the textBox value into the correct field (amount_paid or amount_returned) of the table.

 

You never show us where you want this OR where you get the info.

 

ALSO starting a php section off with <? is BAD programming This is a short tag and that is not always allowed use the real tag <?php and you'll never worry about this gotcha.

 

First you need to do a query to get your information and you need to limit this query to one row ie:

<?php
//connect to db here
$query = "SELECT Amount_paid, Amount_returned FROM banks WHERE [you have to figure this one]";

$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Amount_paid = $row[0];
$Amount_returned = $row[1];
?>

 

This goes into a file that contains your HTML to display this:

 

<fieldset>
<legend>A Name for the Section:</legend>
Amount_paid:<input type="radio" name="group_1" value="Amount_paid" onclick="show('<?php echo $Amount_paid; ?>')"; /><br />
Amount_returned:<input type="radio" name="group_1" value="Amount_returned" onclick="show('<?php echo $Amount_returned; ?>');"  />
<input type="text" id="group_1" />
</fieldset>

 

which calls a javascript:

<script type="text/javascript">
function show(what)
{
document.getElementById('group_1').value = what;
}
</script>

 

put together they look a little like this:

A php file:

<?php
//connect to db here
$query = "SELECT Amount_paid, Amount_returned FROM banks WHERE [you have to figure this one]";

$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Amount_paid = $row[0];
$Amount_returned = $row[1];
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>New document</title>
<meta name="generator" content="TSW phpCoder 2008" />
</head>

<body>
<table align="center" width="700px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><form name="bank" method="post" action="test2.php">
<div>
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" /><br />
List:
<select name="gender">
<option value="">Select Gender</option>
  <option value="Male">Male</option>
  <option value="test">Female</option>
</select>
submitter:
<select style="width: 150px" name="submitter">
<option value="">Select Submitter</option>
<option value="staff1">staff1</option>
<option value="staff2">staff2</option>
</select>
choosebank:
<select name="choosebank">
<option value="">Select Bank</option>
<option value="Bank1">Bank1</option>
<option value="Bank2">Bank2</option>
<option value="Bank3">Bank3</option>
</select>

<fieldset>
<legend>A Name for the Section:</legend>
Amount_paid:<input type="radio" name="group_1" value="Amount_paid" onclick="show('<?php echo $Amount_paid; ?>')"; /><br />
Amount_returned:<input type="radio" name="group_1" value="Amount_returned" onclick="show('<?php echo $Amount_returned; ?>');"  />
<input type="text" id="group_1" />
</fieldset>
<input type="submit"/>

</div>
</form>
</td>
</tr>
</table>
<script type="text/javascript">
function show(what)
{
document.getElementById('group_1').value = what;
}
</script>
</body>
</html>

Link to comment
Share on other sites

Thanks a lot for the reply, I think I haven't explain myself clearly.

 

I have a form with 3 dropdown lists (submitter, choosebank and choosetype), 1 textbox (where a user will be entering an amount) and 2 radio buttons (amount_paid and amount_returned)  depending on which the entered amount is inserted into one of the fields of the database table. let's say user enters an amount into the textbox and selects amount paid radio button in this case the amount should go to amount paid field and amount returned filed is equal to "0".

 

You already helped, but I did not explain it in details and would greatly appreciate your help again!

 

Thanks!

Link to comment
Share on other sites

Here's what I did. Changed the <fieldset> so you now have a place to enter an amount of money and two radio buttons to tell you what the money is applied to.

 

Next I added recognition of this to your php file in the manner you have been using. Notice you might want to put checks on the inputs. The money may have dollar or pound signs and may have decimals or not. I also commented the query out and replaced with an echo for testing. I also left the column name and the money add to that column as strings so the string determines the column.

HTML FORM:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>New document</title>
<meta name="generator" content="TSW phpCoder 2008" />
</head>

<body>
<table align="center" width="610px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><form name="bank" method="post" action="test2.php">
<div>
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" /><br /><br />
List:
<select name="gender">
<option value="">Select Gender</option>
  <option value="Male">Male</option>
  <option value="test">Female</option>
</select>
submitter:
<select style="width: 150px" name="submitter">
<option value="">Select Submitter</option>
<option value="staff1">staff1</option>
<option value="staff2">staff2</option>
</select>
choosebank:
<select name="choosebank">
<option value="">Select Bank</option>
<option value="Bank1">Bank1</option>
<option value="Bank2">Bank2</option>
<option value="Bank3">Bank3</option>
</select><br /><br />

<fieldset>
<legend>Dealing with Money Section:</legend>
Enter an Amount of Money: <input type="text" name="money_value" /><br /><br />
<input type="radio" name="group_1" value="Amount_paid" /> This is the Amount_paid.<br />
<input type="radio" name="group_1" value="Amount_returned" /> Or it is the Amount_returned.
</fieldset>
<input type="submit"/>

</div>
</form>
</td>
</tr>
</table>
<script type="text/javascript">
function show(what)
{
document.getElementById('group_1').value = what;
}
</script>
</body>
</html>

 

test2.php:

<?php

include ("blocks/db.php");

iif(isset($_POST["group_1"]))
{
$money_column = $_POST["group_1"];
$money_amount = $_POST["money_value"];
if($money_column == '')
{
	unset($money_column);
	echo "<p>Please select a radio button.</p>";die;
}
if($money_amount == '')
{
	unset($money_amount);
	echo "<p>Please enter an amount of money.</p>";die;
}
}

if (isset($_POST['firstname']))
{
$firstname = $_POST['firstname'];
if ($firstname == '')
{
	unset($firstname);
}
}

if (isset($_POST['lastname']))
{
$lastname = $_POST['lastname'];
if ($lastname == '')
{
	unset($lastname);
}
}
if (isset($_POST['age']))
{
$age = $_POST['age'];
if ($age == '')
{
	unset($age);
}
}
$myoption = $_POST['gender'];
$sub = $_POST['submitter'];
$bank = $_POST['choosebank'];

if (isset($firstname) && isset($lastname) && isset($age) && ($myoption) && ($sub) && ($bank))
{
$result = 'true';
//$result = mysql_query ("INSERT INTO banks (firstname,lastname,age,gender,submitter,choosebank,$money_column) VALUES ('$firstname','$lastname','$age','$myoption','$sub','$bank', $money_amount)");
echo "INSERT INTO banks (firstname,lastname,age,gender,submitter,choosebank,$money_column) VALUES ('$firstname','$lastname','$age','$myoption','$sub','$bank', $money_amount)";
if ($result == 'true')
{
	echo "<p>Data has been successfully added!</p>";
}
else
{
	echo "<p>Error occured. Data hasn't been added!</p>";
}
}
else
{
echo "<p>Some of the fields are not filled out. Please enter the required data.</p>";
}
?>

 

This is not useable code it is for education only. The 'die' in the top php section will not reload the main form page.

Link to comment
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.