PHP: cannot retrieve data from radio button POST - php

I'm having issues with retrieving data from the radio button. I can retrieve other data from the other html tags but in radio button I cannot. Why is that? Can you help me?
My HTML code is here:
<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
<input type="hidden" name="action" value="mark_payment">
<input type="hidden" name="client-id" value="{$details[row]->id}">
<input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
<input type="hidden" name="period" value="{$details[row]->period}">
<table width="100%" border="0" style="padding-top: 10px;">
<tr align="center">
<td class="radiobut">
<input type="radio" id="choice1" name="payment_status" value="paid"> Paid</td>
<td class="radiobut">
<input type="radio" id="choice2" name="payment_status" value="declined"> Declined</td>
</tr>
<tr align="center">
<td colspan="2">
<div class="paddingRow3">
<select class="selectbut" id="payments" name="payment-method">
<option value="Cheque">Cheque</option>
<option value="Deposit">Deposit</option>
<option value="Cash">Cash</option>
</select>
</div>
</td>
</tr>
<tr align="right">
<td colspan="2">
<div class="paddingRow3">
Update
</div>
</td>
</tr>
</table>
</form>
While my PHP script is here:
if($_REQUEST['action'] == 'mark_payment'){
echo '<pre>';
print_r($_REQUEST);
exit;
}
My output will be just like this:
Array
(
[NONCE] => f305790c4d8b060121b99fe84a8fdf1a62321b3b06b9097caa8439e2f9c5bae7
[action] => mark_payment
[client-id] => 2699422
[invoice-id] => 13008351
[period] => 11
[payment-method] => Cheque
)

You have duplicate name attributes of payment_status for your two choices. They need to be different, or only the last input element (#choice2) will be available.
I've gone with payment_status_paid and payment_status_declined in the following example, but you can also use the square bracket notation name="payment_status[]" to make an array of POST data.
<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
<input type="hidden" name="action" value="mark_payment">
<input type="hidden" name="client-id" value="{$details[row]->id}">
<input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
<input type="hidden" name="period" value="{$details[row]->period}">
<table width="100%" border="0" style="padding-top: 10px;">
<tr align="center">
<td class="radiobut">
<input type="radio" id="choice1" name="payment_status_paid" value="paid"> Paid</td>
<td class="radiobut">
<input type="radio" id="choice2" name="payment_status_declined" value="declined"> Declined</td>
</tr>
<tr align="center">
<td colspan="2">
<div class="paddingRow3">
<select class="selectbut" id="payments" name="payment-method">
<option value="Cheque">Cheque</option>
<option value="Deposit">Deposit</option>
<option value="Cash">Cash</option>
</select>
</div>
</td>
</tr>
<tr align="right">
<td colspan="2">
<div class="paddingRow3">
Update
</div>
</td>
</tr>
</table>
</form>
To find which one is chosen, use a conditional:
if($_REQUEST['action'] == 'mark_payment'){
echo '<pre>';
print_r($_REQUEST);
if ($_POST['payment_status_paid']) {
// Payment was successful
}
else {
// Payment was declined
}
exit;
}
Hope this helps!

Related

Save multiple field in db from dropdown selection

this is what i successfully done.
User key in the data into the text field and preview back the data.
this is what i failed to do:
capture back the data from db because the data that i select from drop down didn't store in the db. thats why i cannot print back the page after clicked submit.
it works well for other field but not for my drop down selection. is there somethings wrong with my code for this part?
this is my index.html file
<tr>
<td colspan="4" align="left" class="form-group form-inline"><b>Section
B</b></td>
</tr>
<script type="text/javascript">
function show() {
var dropdown = document.getElementById("employment");
var current_value =
dropdown.options[dropdown.selectedIndex].value;
if(current_value =="--Choose one--"){
didfv3.style.display = 'none';
didfv2.style.display = 'none';
}
if(current_value == "Unemployed"){
didfv3.style.display = 'none';
didfv2.style.display = '';
}
if(current_value == "Working"){
didfv3.style.display = '';
didfv2.style.display = 'none';
}
}
</script>
<td>employment</td>
<td colspan="4">
<select name="employment" id="employment" onChange="show();">
<option value="--Choose one--">--Choose one--</option>
<option value="Unemployed">Unemployed</option>
<option value="Working">Working</option>
</select>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td width="50%">Balance</td>
<td width="1%" align="center">:</td>
<td width="73%" ><input type="text" name="balance" readonly maxlength="10"
value="<?php echo $balance?>" size="5" > months</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="left"
style="display:none">
<tr>
<td class="form-group form-inline">Basic Salary</td>
<td width="0.5%" align="knk" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="basic" type="text" id="basic" maxlength="20"/></td>
</tr>
<tr>
<td>Gross Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="gross" type="text" id="gross" maxlength="20"/></td>
</tr>
<tr>
<td class="form-group form-inline">Nett Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="nett" type="text" id="nett" maxlength="20"/></td>
</tr>
<tr>
</table>
here is my insert.php
<?
$employment = $_POST['xxx'];
if (isset($_POST['xxx'])) {
$employment =$_POST['xxx'];
}
if($employment =='Working'){
$basic = $_POST['basicsalary00'];
$gross = $_POST['grosssalary00'];
$nett = $_POST['nettsalary00'];
}
elseif($xxx =='Unemployed'){
$balance = $_POST['balance'];
}
$sqlinsert = "INSERT INTO application
(
xxx,basicsalary00,grosssalary00,nettsalary00)
VALUES(0,'$xxx','$basic','$gross','$nett')";
?>
// here is to print back the output
<form name="formName" action="form1" method="post" class="form-group form-
inline">
<tr>
<td rowspan="2">Employment</td>
<td colspan="3">
<option value="<?php echo $employment;?>"><?php echo $employment;?>
</option>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td colspan="3">
<input type="hidden" id="balance" value="<?php echo $balance;?>" />
</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="center"
style="display:none">
<tr></tr>
<tr>
<td>Basic Salary</td>
<td>
<input name="basic" type="hidden" id="basic" value="<?php echo $basic;?>" />RM <?php echo $basic;?></td>
</tr>
<tr>
<td>Gross Salary</td>
<td>
<input name="gross" id="gross" type="hidden" value="<?php echo $gross;?>"
/>RM <?php echo $gross;?></td>
</tr>
<tr>
<td>Nett Salary</td>
<td>
<input name="nett" type="hidden" id="nett" value="<?php echo $nett;?>"
/>RM <?php echo $nett;?></td>
</tr>
</table>
</td>
</tr>
</form>
So there will be 4 pages,
1) for user input (index.html)
2) Preview back the input (i didnt include the file in here - successful)
3) Insert the input to db (insert.php)
4) Print back the result (code at the bottom of the insert.php...fetch from db- which i failed. Working for other field but not for drop down section)
I really need your help.. i've been stuck here for a week.... thank you
A proper dropdown should have a <select> and the <option> elements should be inside it!
In your insert.php, I can see that you have only used this line:
<option value="<?php echo $employment;?>"><?php echo $employment;?></option>
How about your try it like this:
<select readonly>
<option value="--Choose one--">--Choose one--</option>
<option <?php echo ($employment == 'Unemployed') ? 'selected' : ''; ?> value="Unemployed">Unemployed</option>
<option <?php echo ($employment == 'Working') ? 'selected' : ''; ?> value="Working">Working</option>
</select>
Here, we are checking whether any of those <option> element is same as the one that the user has submitted. If so, make it as selected
Hope it helps.
EDIT
If you want to store the value of $employment to display it later, you definitely have to store it in your db.

POST var_dump not showing anything

I'm obviously missing something as.
When I'm trying to post from a form using method POST and var_dumping the results I'm not getting anything back should the var_dump be showing me an array containing the form data?
here's my code so far:
<?php
include "./includes/config.php";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
//mysql_query("");");
header('Location: ./manage_adduser.php');
exit();
}
?>
</head>
<body>
<?php var_dump($_POST); ?>
<div class="adminWrapper">
<div class="adminContentWrapper">
<h1><b>Add a User</b></h1>
<div class="list">
<table class="addNewTable">
<tr>
<td class="addNewLabelRow">
<form method="POST" action="">
<label>User privileges: </label>
</td>
<td class="addNewFieldRow">
<select name="newUserLevel">
<option value=\"1\" >(1) Standard User</option>
<option value=\"2\" >(2) Moderator</option>
<option value=\"3\" >(3) Admin</option>
</select>
</td>
</tr>
<tr>
<td class="addNewLabelRow">
<label>User Login Name: </label>
</td>
<td class="addNewFieldRow">
<input type="name" name="newUserLogin" value="">
</td>
</tr>
<tr>
<td class="addNewLabelRow">
<label>Users Full Name: </label>
</td>
<td class="addNewFieldRow">
<input type="name" name="newUserName" value="">
</td>
</tr>
<tr>
<td class="addNewLabelRow">
<label>Users e-mail address: </label>
</td>
<td class="addNewFieldRow">
<input type="email" name="newUserEmail" value="">
</td>
</tr>
</table>
</div>
<div class="list">
<table class="addNewTable">
<tr>
<td class="addNewLabelRow">
<label>Password: </label>
</td>
<td class="addNewFieldRow">
<input type="password" name="newUserPassword" value="">
</td>
</tr>
<tr>
<td class="addNewLabelRow">
<label>Repeat Password: </label>
</td>
<td class="addNewFieldRow">
<input type="password" name="newUserPasswordRepeat" value="">
</td>
</tr>
</table>
</div>
</div>
<!-- BUTTONS -->
<div class="buttonWrap">
<input class="buttonAddNewGroup" type="submit" value=" Add User ">
</div>
This happens because you're redirecting the browser if the request method is post and you lose the POST-contents.
Comment out this part:
if ($_SERVER["REQUEST_METHOD"] == "POST"){
//mysql_query("");");
header('Location: ./manage_adduser.php');
exit();
}
and var_dump($_POST) will look different.
The var_dump is not executed, because you are exiting your code before that statement:
if ($_SERVER["REQUEST_METHOD"] == "POST"){
/* … */
exit();
}

Get text from silenium php-webdriver from hidden form value

I am using php-webdriver (facebook) and wait 90 seconds for a webpage where name="mdn" is found by following code. This works well but after this I would like to get the text of name="mdn". The hidden form field has a value but on $number there is no value. Does somebody know whats wrong?
Test.php:
$driver->wait(90, 10000)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::name('mdn')
)
);
$number= $driver->findElement(WebDriverBy::name('mdn'))->getText();
Page:
<div id="main content" align="center" class="mainCon">
<div class="mainBG1">
<table width="900" cellspacing="0" cellpadding="0">
<tr>
<td width="650" valign="top">
<div class="paddingT20 paddingB20" style="font-family:'Oswald', sans-serif; font-weight:normal; font-size:35px">xxxxxxx Completed</div>
<form method="post" action="xxxxxxxxxxxxxxxxxxxxxx" target="_blank">
<input type="hidden" name="trans_id" value="1234567" />
<input type="hidden" name="mdn" value="1234567890" />
<input type="hidden" name="bal" value="" />
<input type="hidden" name="exp" value="" />
<input type="hidden" name="note" value="" />
<table width="40%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" class="dv-R"> </td>
</tr>
<tr>
<td width="40%" class="dv-L">MDN</td>
<td width="60%" class="dv-R">6142086840</td>
</tr>
<tr>
<td width="32%" class="dv-L"> </td>
<td width="68%" class="dv-R"><input type="image" value="Print" src="images/xxxxx.png" /></td>
</tr>
</table>
<div class="InfoGr paddingT20 paddingB20"></div><br/>
</p>
</form>
<div class="paddingB40"></div>
</td>
<td valign="top" bgcolor="#e2e2e2"> </td>
</tr>
</table>
</div>
</div>
You can use getAttribute(), because your page have atrribute value and not text.
e.g.
$number= $driver->findElement(WebDriverBy::name('mdn'))->getAttribute('value');

Check drop down and create if statement based on that selection

I want to create a php variable depending on what was selected in a drop down.
This is the drop down:
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
Depending if "per hour" or "per annum" was selected, I want to do the following (I'm not quite sure syntax wise if this is correct and this part is on another page):
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
How can this be done?
Here's my full form:
<form action="<?php the_permalink(); ?>calculations" method="post">
<h2>Savings calculator</h2>
<div class="calculator-divide"></div>
<table border="0">
<tr>
<td colspan="3"><h3>Current service costs</h3></td>
</tr>
<tr>
<td width="440"><p>Pharmacist</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="22.00" type="text" name="pharmacist" />
</p></td>
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
</tr>
<tr>
<td><p>Dispenser / Technician</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="8.00" type="text" name="dispenser" />
</p></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><h3>Time taken to carry out manual dispensing tasks</h3></td>
</tr>
<tr>
<td><p>Measure 50mls dose by hand including Pharmacist check</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="1" type="text" name="measure-check" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Preparing labels from dispensary system</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="0.5" type="text" name="labels" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Write up CD register</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="2" type="text" name="cd-register" />
Minute(s)</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><div class="estimate">
<input style="margin-bottom:20px;" type="submit" value="Estimate my savings" />
</div></td>
</tr>
</table>
</form>
<select id="ddlTime" name="ddlTime">
and
if( $_POST['ddlTime']=='perhour' ){
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
}elseif( $_POST['ddlTime']=='perannum' ){
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
}
I hope this help you :
<?php
if(isset ($_POST['save']))
{
$temp = $_POST['opsi'];
echo $temp;
}
?>
<html>
<body>
<form action="newEmptyPHP.php" method="POST">
<h3> Choose Your Option</h3>
<select name="opsi">
<option value=0 selected>- Customer -</option>
<option value="perannum" name="perannum">perannum</option>
<option value="perhour" name="perhour">perhour</option>
</select> <br> <br>
<input type="submit" name="save" value="Save and Commit">
</form>
</body>
</html>

form 2 button + javascript issue

<form action="index.php?page=checkin" method="post" name="regForm">
<div id="First_1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Fullständiga namn:
</td>
<td>
<input name="full_name" type="text" id="full_name" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Email:
</td>
<td>
<input name="usr_email" type="text" id="usr_email3" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Sex:
</td>
<td>
<select name="sex">
<option value="male">
Kille
</option>
<option value="female">
Tjej
</option>
</select>
</td>
</tr>
<td>
<input type="submit" id="continue" value="Fortsätt">
</td>
</table>
</td>
</tr>
</table>
</div>
<div id="Next_2" style="display: none">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Lösenord:
</td>
<td>
<input name="pwd" type="password" class="required password" id="pwd">
</td>
<td>
En gång till..
</td>
<td>
<input name="pwd2" id="pwd2" class="required password" type="password">
</td>
<td>
<input name="doRegister" type="submit" id="doRegister" value="Register">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(function() {
$("#continue").click(function() {
$.ajax({
type: "POST",
url: "dbc.php",
data: {
check: 'First',
full_name: $('#full_name').val(),
usr_email: $('#usr_email3').val()
},
success: function(msg) {
if (msg == '1') {
$("#First_1").hide();
$("#Next_2").toggle();
} else {
alert(msg);
}
}
});
return false;
});
});
</script>
Thats my form. You fill 3 form elements to start with, then you press the Continue button, and then it sends ajax call to check the fields. If everythings OK (if output = 1 from ajax call), Next_2 gets toggled on, where you enter your password twice. After that you press on the final button Register, which should do "action=index.php?page=checkin", but it doesnt, when i press register it just get blank, and i can see in firebug that it sends another ajax call which i cannot understand because the button doesnt have id="continue" but id="doRegister"
Since the "Continue" button doesn't actually submit the form, try declaring it as a normal button instead of a submit button, like this:
<input type="button" id="continue" value="Fortsätt">
I'm guessing that having two submit buttons on the same form is somehow causing interactions with the handler.
I think you should make the "Continue" button as type="button" and not submit
<input type="button" id="continue" value="Fortsätt">

Categories