I need to collect an input ('x_amount') with a php form, then post it to a page that generates a hash and then posts it to a third party site. I don't want the customer to have a two step process, so can this be done with ajax and php? Here's what I have so far, but it needs to be ajaxified (i think?)
<form action="https://globalgatewaye4.firstdata.com/pay" method="POST">
<?php
$x_login = "xxx-xxx"; // Take from Payment Page ID in Payment Pages interface
$transaction_key = "xxxxxxx"; // Take from Payment Pages configuration interface
$x_currency_code = "CAD"; // Needs to agree with the currency of the payment page
srand(time()); // initialize random generator for x_fp_sequence
$x_fp_sequence = rand(1000, 100000) + 123456;
$x_fp_timestamp = time(); // needs to be in UTC. Make sure webserver produces UTC
// The values that contribute to x_fp_hash
$hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;
$x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);
echo ('<label>x_login</label><input name="x_login" type="hidden" value="' . $x_login . '">' );
echo ('<label>x_amount</label><input name="x_amount" type="hidden" value="' . $x_amount . '">' );
echo ('<label>x_fp_sequence</label><input name="x_fp_sequence" type="hidden" value="' . $x_fp_sequence . '">' );
echo ('<label>x_fp_timestamp</label><input name="x_fp_timestamp" type="hidden" value="' . $x_fp_timestamp . '">' );
echo ('<label>x_fp_hash</label><input name="x_fp_hash" type="hidden" value="' . $x_fp_hash . '" size="50">' );
echo ('<label>x_currency_code</label><input name="x_currency_code" type="hidden" value="' . $x_currency_code . '">');
?>
<input type="hidden" name="x_show_form" value="PAYMENT_FORM"/>
<input type="hidden" name="x_test_request" value="FALSE"/>
Enter Payment Amount:<br>
<input type="text" name="x_amount"/>
<input type="submit" value="Pay with Payment Pages"/>
</form>
In other words, can I collect the x amount, generate the hash and then post it to the third party with just one click of the submit button? How can I accomplish this?
Ok, so I saw that firstdata.com actually suggests you to do that. I've managed to make something for you.
You have to use ajax, post to same page the amount the client entered, process it and return to form. Sumbit form only when data is processed.
I made you entire script because it was getting frustrating at some point figuring out how to do it.
test.php
<?php
if(isset($_POST['amount']) && ($_POST['amount'] != '')){
$x_amount = $_POST['amount'];
$x_login = "xxx-xxx"; // Take from Payment Page ID in Payment Pages interface
$transaction_key = "xxxxxxx"; // Take from Payment Pages configuration interface
$x_currency_code = "CAD"; // Needs to agree with the currency of the payment page
srand(time()); // initialize random generator for x_fp_sequence
$x_fp_sequence = rand(1000, 100000) + 123456;
$x_fp_timestamp = time(); // needs to be in UTC. Make sure webserver produces UTC
// The values that contribute to x_fp_hash
$hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;
$x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);
$values = array('login' => $x_login, 'amount' => $x_amount, 'sequence' => $x_fp_sequence, 'timestamp' => $x_fp_timestamp, 'hash' => $x_fp_hash, 'currency' => $x_currency_code);
echo json_encode($values);
die;
}
?><html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
var processed = false;
function makeProcessed(type){
processed = type;
}
function prepareForm(){
$('#submitPayment').val('Please wait ...');
var processPayment = $.ajax({
type: 'POST',
url: "test.php",
data: {"amount": $('#x_my_amount').val()},
dataType: 'json',
async: false,
success: function(data) {
$('#x_login').val(data.login);
$('#x_amount').val(data.amount);
$('#x_fp_sequence').val(data.sequence);
$('#x_fp_timestamp').val(data.timestamp);
$('#x_fp_hash').val(data.hash);
$('#x_currency_code').val(data.currency);
if(data.hash){
makeProcessed(true);
}
}
});
return processed;
};
</script>
</head>
<body>
<form action="https://globalgatewaye4.firstdata.com/pay" method="POST" id="payment" onsubmit="return prepareForm();">
<input type="hidden" name="x_show_form" value="PAYMENT_FORM"/>
<input type="hidden" name="x_test_request" value="FALSE"/>
<input name="x_login" id="x_login" type="hidden" value="">
<input name="x_amount" id="x_amount" type="hidden" value="">
<input name="x_fp_sequence" id="x_fp_sequence" type="hidden" value="">
<input name="x_fp_timestamp" id="x_fp_timestamp" type="hidden" value="">
<input name="x_fp_hash" id="x_fp_hash" type="hidden" value="" size="50">
<input name="x_currency_code" id="x_currency_code" type="hidden" value="">
Enter Payment Amount:<br>
<input type="text" name="x_my_amount" id="x_my_amount" />
<input type="submit" id="submitPayment" value="Pay with Payment Pages" />
</form>
</body>
</html>
I think you can do this by creating ajax-request on blur event with "x_amount" field.
if generating hash requires php then it will look like this:
form:
Enter Payment Amount:<br>
<input type="text" name="real_amount" id="real-amount"/>
<input type="hidden" name="x_amount" id="x-amount" />
javascript:
$('#real-amount').on('blur', function() {
$.ajax({
....
success: function(data) {
// returned hash should be put in x_amount field here
$('#x-amount').val(data);
}
});
});
Related
I have a dynamic form that has all dynamic arrays being set to my php mysql insert script. The records only get inserted correctly if I have all of my radio buttons checked. If I leave any unchecked random records get inserted.
It's an attendance table and the inputs below repeat for each person. I'm checking their option with the radio button.
I've looked at the js hack of assigning a hidden input and disabling it if one of my radio buttons get checked but I find that a little weird.
My radio buttons have a value of 1 attend or 2 dive and I'm checking for that value in my script. I have a +1 set on the [$key+1] but that only works if all radios are checked. I appreciate any help pointing me in the correct direction as to setting my loop to only grab the data in the array when the radio is checked.
Thanks for looking
The two offending radio buttons are setup like this with the rest off the variables being set from hidden fields and are all filled with actual values. Everything gets insert if I don't use radio buttons in the form or if they're all checked.
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$dive_points.'">
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$attend_points.'">
<input type="hidden" name="_rowid_[]" value="' .$row1['_rowid_']. '">
<input type="hidden" name="location_date[]" value="' .$location_date. '">
<input type="hidden" name="location_name[]" value="' .$location_name. '">
<input type="hidden" name="first_name[]" value="' .$row1['first_name']. '">
<input type="hidden" name="last_name[]" value="' .$row1['last_name']. '">
<input type="hidden" name="cert_agency[]" value="' .$row1['cert_agency']. '">
<input type="hidden" name="high_cert[]" value="' .$row1['high_cert']. '">
<input type="hidden" name="email[]" value="' .$row1['email']. '">
<input type="hidden" name="phone_number[]" value="' .$row1['phone_number']. '">
<input type="hidden" name="photo_release[]" value="' .$row1['photo_release']. '">
<input type="hidden" name="diver_pic[]" value="' .$row1['diver_pic']. '">
<input type="hidden" name="submitted_email[]" value="' . $submitted_email . '">
<input type="hidden" name="food_option[]" value="' .$row2['food_option']. '">
My php loop is setup like this.
foreach ($_POST['location_date'] as $key => $value) {
$_rowid_ = $_POST['_rowid_'][$key];
$location_date = $_POST['location_date'][$key];
$location_name = $_POST['location_name'][$key];
$first_name = $_POST['first_name'][$key];
$last_name = $_POST['last_name'][$key];
$cert_agency = $_POST['cert_agency'][$key];
$high_cert = $_POST['high_cert'][$key];
$email = $_POST['email'][$key];
$phone_number= $_POST['phone_number'][$key];
$photo_release = $_POST['photo_release'][$key];
$diver_pic = $_POST['diver_pic'][$key];
$submitted_email = $_POST['submitted_email'][$key];
$food_option = $_POST['food_option'][$key];
foreach ($_POST['dive_attend_points'][$key+1] as $row) {
$dive_attend = $row['dive_attend'];
if (!empty($dive_attend)) {
if($dive_attend == 1) {
$dive_points = 0;
$attend_points = 1;
} elseif($dive_attend == 2) {
$dive_points = 2;
$attend_points = 0;
}
}
}
if($dive_attend == 1 || $dive_attend == 2){
//mysql insert here.
}
}
I'm not sure why I've never seen or heard of this until I saw it in a post while doing research. To keep your arrays aligned with the same indexes is you assign an index dynamically or manually. In my case I used the same '.$row1['_rowid_'].' I was using to give my radio button group a unique name so they grouped properly. I placed it inside the empty [] for each input. This technique allowed me to not use those clunky js hacks.
HTML
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$dive_points.'">
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$attend_points.'">
<input type="hidden" name="_rowid_['.$row1['_rowid_'].']" value="' .$row1['_rowid_']. '">
<input type="hidden" name="location_date['.$row1['_rowid_'].']" value="' .$location_date. '">
<input type="hidden" name="location_name['.$row1['_rowid_'].']" value="' .$location_name. '">
<input type="hidden" name="first_name['.$row1['_rowid_'].']" value="' .$row1['first_name']. '">
<input type="hidden" name="last_name['.$row1['_rowid_'].']" value="' .$row1['last_name']. '">
<input type="hidden" name="cert_agency['.$row1['_rowid_'].']" value="' .$row1['cert_agency']. '">
<input type="hidden" name="high_cert['.$row1['_rowid_'].']" value="' .$row1['high_cert']. '">
<input type="hidden" name="email['.$row1['_rowid_'].']" value="' .$row1['email']. '">
<input type="hidden" name="phone_number['.$row1['_rowid_'].']" value="' .$row1['phone_number']. '">
<input type="hidden" name="photo_release['.$row1['_rowid_'].']" value="' .$row1['photo_release']. '">
<input type="hidden" name="diver_pic['.$row1['_rowid_'].']" value="' .$row1['diver_pic']. '">
<input type="hidden" name="submitted_email['.$row1['_rowid_'].']" value="' . $submitted_email . '">
<input type="hidden" name="food_option['.$row1['_rowid_'].']" value="' .$row2['food_option']. '">
The adjusted php
foreach ($_POST['location_date'] as $key => $value) {
$_rowid_ = $_POST['_rowid_'][$key];
$location_date = $_POST['location_date'][$key];
$location_name = $_POST['location_name'][$key];
$first_name = $_POST['first_name'][$key];
$last_name = $_POST['last_name'][$key];
$cert_agency = $_POST['cert_agency'][$key];
$high_cert = $_POST['high_cert'][$key];
$email = $_POST['email'][$key];
$phone_number= $_POST['phone_number'][$key];
$photo_release = $_POST['photo_release'][$key];
$diver_pic = $_POST['diver_pic'][$key];
$submitted_email = $_POST['submitted_email'][$key];
$food_option = $_POST['food_option'][$key];
$dive_attend = $_POST['dive_attend_points'][$key]
if($dive_attend == 1) {
$dive_points = 0;
$attend_points = 1;
} elseif($dive_attend == 2) {
$dive_points = 2;
$attend_points = 0;
}
//mysql insert here.
} //end of for each
I try to send couple values from html form into database but variables are empty.
If I print variables in php area like echo $ad1_uid; I get the value.
After sending - all values are empty:
author_uid=&state=complete&mail=&user_uid=
Where could be a reason?
<form action="" method="get">
<input type="hidden" value="<?php ''.$ad1_uid; ?>" name="author_uid">
<input type="hidden" value="complete" name="state">
<input type="hidden" value="<?php ''.$user->mail; ?>" name="mail">
<button name="user_uid" type="submit" value="<?php ''.$user->uid; ?>">Zapisuje się</button>
</form>
<?php
}
$wyslany_user_uid = $_GET['user_uid'];
$wyslany_author_uid = $_GET['author_uid'];
$wyslany_mail = $_GET['mail'];
$wyslany_state = $_GET['state'];
var_dump($wyslany_mail);
mysql_query("INSERT INTO `krajeto_demo`.`registration1`
(user_uid, author_uid, mail, state ) VALUES (
'" . $wyslany_user_uid . "', '" . $wyslany_author_uid. "', '" . $wyslany_mail . "','" . $wyslany_state . "'
");
?>
To output the value of $ad1_uid in the form, try this instead:
<input type="hidden" value="<?= $ad1_uid; ?>" name="author_uid">
Or this if your setting don't
<input type="hidden" value="<?php echo $ad1_uid; ?>" name="author_uid">
Your PHP is not set to echo data into the form.
I've fixed those and added a debug clause to run. Just set to false once you've ran it to output data.
<form action="" method="get">
<input type="hidden" value="<?php echo $ad1_uid; ?>" name="author_uid">
<input type="hidden" value="complete" name="state">
<input type="hidden" value="<?php echo $user->mail; ?>" name="mail">
<button name="user_uid" type="submit" value="<?php echo $user->uid; ?>">Zapisuje się</button>
</form>
<?php
}
$debug = true;
if ($debug === true)
{
echo '<hr />
<h4>Debug Area</h4>
<pre>';
print_r(array($_GET, $_POST));
echo '</pre>
<hr />';
}
$wyslany_user_uid = $_GET['user_uid'];
$wyslany_author_uid = $_GET['author_uid'];
$wyslany_mail = $_GET['mail'];
$wyslany_state = $_GET['state'];
mysql_query("INSERT INTO `krajeto_demo`.`registration1` (user_uid, author_uid, mail, state ) VALUES (
'" . $wyslany_user_uid . "', '" . $wyslany_author_uid. "', '" . $wyslany_mail . "','" . $wyslany_state . "'
");
Try that and let me know what is echo'd to you.
Context:
using PHP to echo HTML.
Issue:
Echoed HTML does not display unless I've hard-coded $n (i.e. $n = 2;).
Trouble-shooting:
-I've confirmed that I'm receiving the POST data via echo,var_dump,print_r.
-I've confirmed that the for loop works by substituting hard-coded numbers for $n.
-I've made sure that the string being received via POST is an integer.
<?php
$n=intval($_POST["a"]);
for($count=1;$count<=$n;$count++)
{
echo '<li>foo ' . $count . ':<input type="text" name="bar' . $count . '" value="baz ' . $count . '"></li>';
};
?>
EDIT: The PHP gets POST from AJAX (see below)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#formid").change(function(){
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
data : $(this).serialize(),
success : function( response ) {
alert( response );
}
});
$("#div1").load("load.php");
});
});
</script>
Edit: As per your originally posted question
You are missing a ' after value="baz
value="baz . $count . '"></li>
^ // right there
Full echo should be:
echo '<li>foo ' . $count . ':<input type="text" name="bar' . $count . '" value="baz' . $count . '"></li>';
This has bitten me before, and what I usually do to concatenate long strings is after each period, I hit enter and create a new line.
Remember, PHP is white-space insensitive so feel free to separate lines all you'd like.
It's unclear as to what your expected result should be, yet using the (fixed) code that follows, produced the following in HTML source:
<li>foo 1:<input type="text" name="bar1" value="baz1"></li>
<li>foo 2:<input type="text" name="bar2" value="baz2"></li>
<li>foo 3:<input type="text" name="bar3" value="baz3"></li>
<li>foo 4:<input type="text" name="bar4" value="baz4"></li>
<li>foo 5:<input type="text" name="bar5" value="baz5"></li>
PHP (using 5 as a number)
Sidenote: I added . "\n" at the end of the code for clarity.
<?php
// $n=intval($_POST["a"]);
$n=intval(5);
for($count=1;$count<=$n;$count++)
{
echo '<li>foo ' . $count . ':<input type="text" name="bar' . $count . '" value="baz' . $count . '"></li>' . "\n";
};
?>
Edit
Successful test with form included:
<?php
if(isset($_POST['submit'])){
// $n=intval(5);
$n=intval($_POST["a"]);
for($count=1;$count<=$n;$count++)
{
echo '<li>foo ' . $count . ':<input type="text" name="bar' . $count . '" value="baz' . $count . '"></li>' . "\n";
};
}
?>
<form method="post" action="">
Number:
<input type="text" name="a">
<br>
<input type="submit" name="submit" value="Submit">
</form>
Sidenote:
Just to test both methods - one with and one without (intval), $n=$_POST["a"]; also worked.
<?php
$value1 = rand (1,10);
$value2 = rand (1,10);
?>
<form action="mathq.php" method="post">
<input type="text" name="input1a" value="<?php echo $value1; ?>" class="value" />
<input type="text" value="x" class="equa" />
<input type="text" name="input2a" value="<?php echo $value2; ?>" class="value" />
<input type="text" value="=" class="equa" />
<input id="answer" type="text" name="answer" value="" class="answer" /><br /><br />
<input type="submit" value="Submit answer" class="submit">
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
</form>
<?php
if(isset($_POST['input1a']) && isset($_POST['input2a']) && isset($_POST['answer'])) {
$result = $_POST['answer'];
$input1a = $_POST['input1a'];
$input2a = $_POST['input2a'];
if (!empty($input1a) && !empty ($input2a) && !empty($result)) {
//echo '<span class="success">Success! ';
}
if ($result == $input1a * $input2a)
{
print($input1a . ' x ' . $input2a . ' = ' . '<span class="correct">' . $result . '</span>' . '<br />Correct!');
}
else
{
print($input1a . ' x ' . $input2a . ' = ' . '<span class="incorrect">' . $result . '</span>' . '<br />Wrong!<br /> The correct answer was: ' . '<span class="correct">' . $input1a * $input2a . '</span>');
}
}
?>
I was wondering how i could calculate total score of right answers after the user hits the submit button? How could i create a loop that would go through the rand number of problems and after it goes through once it exits and outputs the person score as percentage of right or wrong answers. I'm stuck on this part.
I was also thinking of another way i could tackle this problem.
A user goes through the rand number of math problems once he is done with practices problem he or she hits Done button and it outputs his score. Can you give me guidance how i can do this? Thanks.
You need a place to store data whilst questions are in progress.
This would commonly be done in Session data, or in a local database. It could be done in cookies, but not advisable. It could also be done in a flat text file.
You'd need to count how many questions done, how many right answers, and you can then derive the resulting % to feed back to the user.
Without somewhere to store the intermediate data, you cannot do this.
I've never really heard of something this simple working in every browser, but firefox, so can anyone shed some light on this?
reply
and
jQuery:
var nameForm;
function quoteMe(commenterName, id, name) {
if (name == "")
nameForm = 'Name: <input type="text" class="input" name="username" value="" />'
else
nameForm = '<div class="outlineTitle2">'+name+'</div>'+'<input type="hidden" class="input" name="username" value="'+name+'" />'
if (jQuery("#replyForm"+id).length == 0) {
jQuery("#"+id).append('<form method="post" id="replyForm'+id+'"><br /><input type="hidden" name="cid" value="'+id+'" /><input type="hidden" name="nameOfTable" value="articles" />'+nameForm+'<br />Reply: <input type="text" class="input" name="commentReply" value="'+commenterName+'" style="width:80%" /><input type="submit" value="POST" name="addReply" class="bigButton" /></form>');
}
else {
jQuery("#replyForm"+id).remove();
}
}
More updates below under first answer.
http://cl.ly/082z3g04381G3r1i2e08
Try to replace quoteMe by alert and see what happens.If an alert pops up, it means you have a problem in your function.
The inline events work fine on Firefox too, here is common call I use:
label
even shorter:
label
if you use href="#", you need to return false at the onclick call, otherwise, the hash key will change in the address bar. It can be annoying, especially in ajax based apps.
it is often useful to pass to the function this, it will give you the reference to the clicked <a>. You don't have to play with id's to find it back.
You can't put a form as a child of a table element. Either use the table's parent node, or add a cell.
(For reference, a properly indented version of the source:
var nameForm;
function quoteMe(commenterName, id, name) {
if (name == "") {
nameForm = 'Name: <input type="text" class="input" name="username" value="" />'
}
else {
nameForm = '<div class="outlineTitle2">' + name + '</div>' +
'<input type="hidden" class="input" name="username" value="' + name + '" />'
}
if (jQuery("#replyForm" + id).length == 0) {
jQuery("#" + id).append('<form method="post" id="replyForm' + id + '">' +
'<br />' +
'<input type="hidden" name="cid" ' +
' value="' + id + '" />' +
'<input type="hidden" name="nameOfTable" ' +
' value="articles" />' +
nameForm + '<br />' +
'Reply: <input type="text" class="input" ' +
' name="commentReply" ' +
' value="' + commenterName + '"' +
' style="width:80%" />' +
'<input type="submit" value="POST" ' +
' name="addReply" class="bigButton" />' +
'</form>');
}
else {
jQuery("#replyForm" + id).remove();
}
}
).