Transform value of PHP form on submit - php

i'm setting up donate option on a website using przelewy24.pl. They have this startup template to send values by $_GET method to their website.
Everything works fine exept the amount field. Przelewy24 needs a gr amount (like cents) and i would like for the donor to type in integer in full zł (like $).
If the upper is not clear - when i type 100 in the field, it sends it to przelewy24 as 100 gr, whitch will be 1 zł.
I need to know how could i format the amount sent to them as in simple calculation - when 100 is typed, get sends 10000. (x*100)
The form used is shown below. The quick-start guide is avaliable here, but only in polish
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>

You can do it with a simple Javascript code.
You need to capture the value from input, transform it, and put the value on input hidden:
function formatMoney(e) {
document.getElementById('z24_kwota').value = (!isNaN(e.target.value) ? e.target.value : 0) * 100
// just to debug.. you can remove this line:
document.getElementById('final_value').innerHTML = document.getElementById('z24_kwota').value
}
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td>
<input type="hidden" name="z24_kwota" id="z24_kwota">
<input type="text" onkeyup="formatMoney(event)"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<!-- you can remove this line: -->
Final Value: <span id="final_value"></span>

Try changing the value before submitting the form like below,
<form method="get" id="myform" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var firstSubmit = false;
$('#myform').on('submit',function(e){
if(!firstSubmit){
e.preventDefault();
firstSubmit = true;
var amount = parseInt($('input[name=z24_kwota]').val());
$('input[name=z24_kwota]').val(amount*100);
$('#myform').trigger('submit');
}
})
</script>
Note: I have given an id to the form as myform

Related

How to deal with many input fields with the same name, and post to PHP?

Let's say that I have an HTML structure that looks like this. Each left-context-field and right-context-field are related and in the backend it should be possible to link left to its right colleague.
<table>
<tbody>
<tr>
<td>
<input type="text" class="left-context-field" value="First left value">
</td>
<td>
<input type="text" class="right-context-field" value="First right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Second left value">
</td>
<td>
<input type="text" class="right-context-field" value="Second right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Third left value">
</td>
<td>
<input type="text" class="right-context-field" value="Third right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Fourth left value">
</td>
<td>
<input type="text" class="right-context-field" value="Fourth right value">
</td>
</tr>
</tbody>
</table>
and a user can add as many additional rows as they please. How would I go about posting the values to a PHP file?
Should I wrap all rows in one single huge form, and then call .serializeArray()? Or one form per row?
From a data-oriented approach, what's the best way to deal with this? I'll be sending the data to the back-end, but I want to make the data ass easily accessible for the the backend developer as possible.
I've Edited meda's Answer, but he is not approving that Edit, So I'm posting it here again.
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];

Codeigniter post variable with empty value

I have nine text boxes wrapped in a form but when post to codeigniter controller, there are two specific text boxes have no value but they actually have.
Anyone encountered this issue before ? what is actually wrong ?
Form
<form name="frm_RRequest" id="frm_RRequest" action="<?php echo site_url('user/add_recommendation_request/'); ?>" method="post">
<tbody>
<tr>
<td class="col-left">Date</td>
<td class="col-middle"><input class="datepicker" type="text" name="txtStartDate" id="txtStartDate" class="datepicker" placeholder="Click to select a start date.."></td>
<td class="col-middle"><input class="datepicker" type="text" name="txtEndDate" id="txtEndDate" class="datepicker" placeholder="Click to select a end date.."></td>
<td class="col-right"><div class="error" id="error_date"> </div></td>
</tr>
<tr>
<td class="col-left">Travel time</td>
<td class="col-middle"><input type="text" class="ptTimeSelect input" name="txtStartTime" id="txtStartTime" placeholder="Click to select start time.." data-default-time="false"></td>
<td class="col-middle"><input type="text" class="ptTimeSelect input" name="txtEndTime" id="txtEndTime" placeholder="Click to select end time.." data-default-time="false"></td>
<td class="col-right"><div class="error" id="error_time"> </div></td>
</tr>
<tr>
<td class="col-left">Location</td>
<td class="col-middle-2"><input type="text" class="inputWithImge" name="txtStartLocation" id="txtStartLocation" onmouseover="display_text(this)" placeholder="Click the icon to select a start point"/><img src="<?php echo base_url('assets/images/search_icon.png'); ?>" class="location-icon" title="Click to show map" name="location-icon_start" value="StartLocation"/></td>
<td class="col-middle-2"><input type="text" class="inputWithImge" name="txtEndLocation" id="txtEndLocation" onmouseover="display_text(this)" placeholder="Click the icon to select a end point"/><img src="<?php echo base_url('assets/images/search_icon.png'); ?>" class="location-icon" title="Click to show map" name="location-icon_end" value="EndLocation" /></td>
<td class="col-right"><div class="error" id="error_location"> </div></td>
</tr>
<input type="hidden" name="txtStartLocation_Coordinates" id="txtStartLocation_Coordinates">
<input type="hidden" name="txtEndLocation_Coordinates" id="txtEndLocation_Coordinates">
</tbody>
</table>
</div>
<div><input type="button" class="button" id="btnGo" name="btnGo" value="Input detail" /> <span> << click this button if the travel time and location(s) are different for each day</span></div>
<div id="detail">
</div>
<input type="hidden" name="txtTotalDetail" id="txtTotalDetail">
<input type="hidden" name="txtNoOfDays" id="txtNoOfDays">
<div> </div>
<div><input type="button" id="btn_SaveDetail" name="btn_SaveDetail" class="button" value="Save" /></div>
</form>
<script>
function display_text(obj){
var value = obj.value;
obj.title = value;
}
</script>
Controller :
$total_detail = $this->input->post("txtTotalDetail");
$noOfDays = $this->input->post("txtNoOfDays");
$userid = $this->session->userdata('id');
$start_date = $this->input->post("txtStartDate");
$end_date = $this->input->post("txtEndDate");
$start_time = $this->input->post("txtStartTime");
$end_time = $this->input->post("txtEndTime");
$start_location = $this->input->post("txtStartLocation");
$end_location = $this->input->post("txtEndLocation");
$start_location_coor = $this->input->post("txtStartLocation_Coordinates");
$end_location_coor = $this->input->post("txtEndLocation_Coordinates");
These two text boxes have no value :
$start_location = $this->input->post("txtStartLocation");
$end_location = $this->input->post("txtEndLocation");
It is possible you forgot to use $this->form_validation->set_rules() on those particular fields.
Put value in your input tag like
<input type="hidden" name="txtStartLocation_Coordinates" value ="<?php echo $this->input->post('txtStartLocation_Coordinates');?>" id="txtStartLocation_Coordinates">
<input type="hidden" name="txtEndLocation_Coordinates" value ="<?php echo $this->input->post('txtEndLocation_Coordinates');?>" id="txtEndLocation_Coordinates">
<script>
$(function(){
$('#txtStartLocation_Coordinates').val("your value");
$('#txtEndLocation_Coordinates').val("your value");
});
</script>

jQuery validate name array, required name array

I'm running into a bit of trouble with the jQuery validate plugin.
I have a form with some name array checkboxes and corresponding name array radio inputs.
What I'm after is to only validate the action[] inputs when the (not required) check[] input has been checked.
$(document).ready(function(){
$("#testForm").validate({
rules: {
'action[]': {
required: function () {
return $("'check[]':checked");
}
}
}
});
});
<form id="testForm" name="testForm" method="post" action="" >
<table cellpadding="1" cellspacing="1" border="1">
<tr>
<th> </th>
<th>Include</th>
<th>Action</th>
</tr>
<tr>
<th>Row[0]:</th>
<td><input type="checkbox" name="check[0]" value="Y" /></td>
<td>
<input type="radio" name="action[0]" class="required" value="ON" />ON<br/>
<input type="radio" name="action[0]" class="required" value="OFF" />OFF<br/>
<label class="error" for="action[0]" generated="true"></label>
</td>
</tr>
<tr>
<th>Row[1]:</th>
<td><input type="checkbox" name="check[1]" value="Y" /></td>
<td>
<input type="radio" name="action[1]" class="required" value="ON" />ON<br/>
<input type="radio" name="action[1]" class="required" value="OFF" />OFF<br/>
<label class="error" for="action[1]" generated="true"></label>
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
I have set up a jsfiddle to hopefully explain the problem in more detail:
http://jsfiddle.net/h2dt4t30/2/
Any help or guidance would be appreciated.
Thanks in advance
You could try something like
jQuery.validator.addClassRules("action-required", {
required: function(el){
return $(el).closest('tr').find('input[name^="check"]').is(':checked')
}
});
$("#testForm").validate({});
//since snippet doesn't allow form submit
$(':button').click(function(){
console.log($("#testForm").valid())
})
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="testForm" name="testForm" method="post" action="" >
<table cellpadding="1" cellspacing="1" border="1">
<tr>
<th> </th>
<th>Include</th>
<th>Action</th>
</tr>
<tr>
<th>Row[0]:</th>
<td><input type="checkbox" name="check[0]" value="Y" /></td>
<td>
<input type="radio" name="action[0]" class="action-required" value="ON" />ON<br/>
<input type="radio" name="action[0]" class="action-required" value="OFF" />OFF<br/>
<label class="error" for="action[0]" generated="true"></label>
</td>
</tr>
<tr>
<th>Row[1]:</th>
<td><input type="checkbox" name="check[1]" value="Y" /></td>
<td>
<input type="radio" name="action[1]" class="action-required" value="ON" />ON<br/>
<input type="radio" name="action[1]" class="action-required" value="OFF" />OFF<br/>
<label class="error" for="action[1]" generated="true"></label>
</td>
</tr>
<tr>
<td colspan="3"><input type="button" value="Submit Form" /></td>
</tr>
</table>
</form>
jsfiddle seems to be down for me at the moment, so I will update the answer as soon as it's back online
The validation plugin provides the depends option.
Please try like this:
$("#testForm").validate({
rules: {
'action[]': {
depends: function () {
return $("check[]").is(":checked"); //here you need to target the needed checkbox, not all of them
}
}
}
});

input required depending on button of form

To enter data into my database I created this form. The user can enter data add by clicking the add button the information is entered into the database. I am using required to force the user to enter text.
Now I added the edit and delete button. Therefore the user shall select a record with the radio buttons provided. But with the required in the input elements it is only possible to do edit or delete if text is entered.
Is it somehow possible to assign the required to a certain button?
<form>
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input type="text" placeholder="enter" required >
</td>
<td>
<input type="text" placeholder="something" required>
</td>
</tr>
</table>
<input type="submit" value="add" formaction="form1.php" formmethod="post"/>
<input type="submit" value="delete" formaction="form2.php" formmethod="post"/>
<input type="submit" value="edit" formaction="form3.php" formmethod="post"/>
</form>
A way to do this, is without using 'required', but reproducing this html5 effect using event handlers, focus() and alert().
event.preventDefault() prevents the form to submit.
Check it:
<form method="POST">
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input id="enter" type="text" placeholder="enter" >
</td>
<td>
<input id="something" type="text" placeholder="something" >
</td>
</tr>
</table>
<input id="add" formaction="form1.php" formmethod="post"
onclick="
if(document.getElementById('enter').value===''){
event.preventDefault();
document.getElementById('enter').focus();
alert('Please,fill out this field');
} else if(document.getElementById('something').value===''){
event.preventDefault();
document.getElementById('something').focus();
alert('Please,fill out this field');
}"
type="submit" value="add"/>
<input id="delete" type="submit" formaction="form2.php" formmethod="post value="delete"/>
<input id="edit" type="submit" formaction="form3.php" formmethod="post value="edit"/>
</form>

My php code is not executing , exactly means values is not adding into database

<?php
if(isset($_POST['submit']))
{
$uname=$_POST['uname'];
$pswd=$_POST['pswd'];
$cpswd=$_POST['cpswd'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$address=$_POST['add'];
$mobile=$_POST['mobile'];
$con=mysqli_connect("localhost","root","","registration");
$sql1="select usname, email from registration1 where usname='$uname' or email='$email'";
$query=mysqli_query($con,$sql1) or die(mysqli_error($con));
$rownum=mysqli_num_rows($query);
if($rownum != 0)
{
echo "User With This User Name or Email Address Is Already Available";
}
else
{
$sql2="insert into registration1 (usname,psswd,fsname,lsname,email,address,mobileno) values ('$uname','$pswd','$fname','$lname','$email','$address','$mobile')";
mysqli_query($con,$sql2) or die(mysqli_error($con));
echo "Registration Successful";
}
}
?>
I am redirecting to home.php which is written in form action, the php code in this is not executing. can anybody help me to resolve this
this is my html code i am redirecting to home .php on clicking submit button values entered in textbox is not entering into database on clicking submit button.
`<html>
<head>
<title>Registration Form</title>
</head>
<body>
<center>
<form method="post" action="home.php">
<table>
<tr>
<td>
<p>User Name</p>
</td>
<td>
<input type="text" name="uname"/>
</td>
</tr>
<tr>
<td>
<p>Password</p>
</td>
<td>
<input type="password" name="pswd" />
</td>
</tr>
<tr>
<td>
<p>Confirm Password</p>
</td>
<td>
<input type="password" name="cpswd" />
</td>
</tr>
<tr>
<td>
<p>First Name</p>
</td>
<td>
<input type="text" name="fname" />
</td>
</tr>
<tr>
<td>
<p>Last Name</p>
</td>
<td>
<input type="text" name="lname" />
</td>
</tr>
<tr>
<td>
<p>Email Address</p>
</td>
<td>
<input type="email" name="email" />
</td>
</tr>
<tr>
<td>
<p>Address</p>
</td>
<td>
<input type="text" name="add" />
</td>
</tr>
<tr>
<td>
<p>Mobile No.</p>
</td>
<td>
<input type="text" name="mobile" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="submit" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>`
If there is no Output / No Errors .. Please Make Sure $_POST['submit'] Variable is Posted Vai From..
at first want to say please describe the error properly.there can be many reason.
option 1:maybe $_POST['submit'] or other values is not initialized properly.then it wont enter the if block ever
option 2:$con=mysqli_connect("localhost","root","password"); check if this connection establishment is okay or not.
option 3:i am not sure.but i think while fetching multiple column you need to use an array().
please debug the code..find out after which line the code stop executing.you can do this by using echo 'xxx'; as checkpoint in your code and report us
note:use mysqli_real_escape_string() and various php mysqli function to be safe from mysqli injection
<input type="submit" value="submit" name="submit" />
<input type="hidden" name="submit" value="submit" />
</form>
this is how your form should be done..add that hidden type
EDIT 1: if it doesn't work.then put echo 'step 1,2,3...' in your home.php.and check after which statement it stops executing.for exemple you can put a echo right after if() to check if it is entering in your if block or not

Categories