Link to home
Start Free TrialLog in
Avatar of crescue
crescue

asked on

how can I get the value of an input field in HTML and used it to calculate in a php form

How can use the input value of a field form to calculate in a php form.
I want the result of that value to use it to ECHO in php to Paypal

Any help with your own code or suggestions would help

Tnx

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>




</head>
<body>


<form method="POST" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</form>

<p>

<?php
      echo T1 *2;
?>

</body>

</html>
Avatar of Abdul Khaliq Makhdoom
Abdul Khaliq Makhdoom
Flag of Pakistan image

As far as I've understood, you need to get the value of the number from the form, You can get the value by name attribute of the input field in HTML. let's say we have the form in html like this:
<form action="process.php" method="post">
<label>Input a Number</label>
<input type="number" value="number" name="number" />
<input type="submit" value="submit" name="submit" />
</form>

Open in new window


create another file process.php in the same directory and use this code.

<?php 
if(isset($_POST['submit'])){ // check if form is submitted
$number = $_POST['number']; // getting value of number from the form
echo $number;	// printing value of the form.
}
?>

Open in new window


Hope this helps.
Avatar of crescue
crescue

ASKER

It is not working the way I would like. I want that result from my php to go back into my form and post it to paypal like :

<input type="hidden" name="amount" value="<?php echo $number; ?>">

but the only way it works is IF I HAVE THE PHP WITHIN my form NOT by passing it to external php.  If I use the external php then I can't pass the result to paypal (I get an error line number is empty or not valid)

I want YES to get the value of the VARIABLE in my external PHP and use it in my code to pass to paypal

Tnx
Avatar of Jim Riddles
What I am hearing is that you want to set a value for a hidden input before submitting to PayPal.  If that is the case, it will be easiest to use JavaScript.

Check out this JSFiddle.

HTML
<form method="POST" name="form
 action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p>
<label for="t1">T1</label><input type="text" name="T1" id="t1" size="20"><br>
<label for="amount">Amount</label><input type="text" name="amount" id="amount"><br>
<input type="submit" value="Submit" name="B1" id="submit">
<input type="reset" value="Reset" name="B2">
</form>
<p>

Open in new window


JavaScript
document.getElementById("submit").addEventListener("click", function(e){
    e.preventDefault();
    var t1 = document.getElementById("t1");
    var amount = document.getElementById("amount");
    amount.value = t1.value * 2;
    //the following line will actually submit your form when done with the processing
    document.forms["myform"].submit();
});

Open in new window


What I have done is to add an event listener to your submit button.  This listener first prevents your form from submitting, takes the value from T1 and multiplies it by 2 and sets that value to amount.  Although I did not show in the JSFiddle, but did show in the JavaScript in this solution is the final command to actually submit your form.

You haven't gone into any specifics about what you need to accomplish, this should give you the base you need.

If you have any further questions, don't hesitate to ask.
Avatar of crescue

ASKER

Hi, what I am trying to accomplish is set a price that was calculated by ajax and php into the paypal variable for AMOUNT
I do get the results from PHP, and they are posted correctly as
<label for="amount">Amount</label><input type="text" name="amount" id="amount"><br>
The problem is passing that value in HTML to PAYPAL, I am having an issue by getting the variable amount from php and post it into html to be sent to paypal (hidden input)
What you are suggesting sounds like the solution, but I have 0 experience with JavaScript, but willing to learn :-)

What I currently have is the amount correctly posted in id=????

If I can just transfer that amount to the paypal hidden input would be AWESOME

Hope you can help me, I have been trying to find the solution for weeks already

I would need the exact code from java and necessary files or scripts

Tnx again
My apologies, but I am confused.  Do you already have code to retrieve the correct price via AJAX, or not?
Avatar of crescue

ASKER

yes, I can retrieve the price via ajax, and it is already posted in my php file. The problem is that I can not update the hidden link to paypal, I have tried several variables to pass to paypal, and it does not work. The only time it works is IF I POST THE DIRECT AMOUNT INTO PAYPAL FIELD EX. Paypal Amount = "49.99" WORKS   and it DOES NOT WORK if I post Paypal Amount = PHP variable or AJAX variable
In your AJAX function to retrieve the amount, just assign the resulting value to the hidden field.  It would help if you can show relevant code.  What you show in your original question does not show the actual form you are trying to submit, or the AJAX function that retrieves the value.

One key to remember about AJAX is it is asynchronous.  Meaning that it will begin its function and allow other processes to continue.  So, if your code is calling an AJAX function on form submission, and you are not preventing the default behavior, your form will likely submit before your AJAX value actually retrieves the result.

Like I said...I need to see some relevant code to really be helpful.
Avatar of crescue

ASKER

Sorry, I am attaching the php file along with php code

What I want is to paste the TOTAL price when they press BUY NOW button

Tnx
calc.php
testphp.php
In your AJAX function in the success return, add the following line below "$('#total-total').text(result.total);".
$('#amount').text(result.total);

Open in new window

Change the hidden field for amount to the following:
<input type="hidden" id="amount" name="amount" value="">

Open in new window

For testing, you can temporarily change the amount field type to text, instead of hidden, so you can see the value change on the fly, but be sure to set it back to hidden for production.

What I have done here it to add an ID to the amount field, making it easy to target with jQuery.  Then, the extra line we added in your success callback finds that input and changes the value to the total value returned by your PHP script.
Avatar of crescue

ASKER

Hi, tnx for your help. i'vr tried and it changes the value 'on the fly' of Total New (just add it to test), and YES it updates the value in Total New BUT NOT in the paypal field (in paypal is just blank, and yes it is that field because if I add a value IT will be displayed while loading the page BUT IT DOES NOT UPDATE

Tnx for reviewing what I did  I uploaded the file
testphp.php
Okay, so you have verified that the value that is returned from your AJAX function is populating the hidden input fields?  It may help if you output the value of the result object to the console.  Immediately after the line "success: function (result) {", add the following:
console.log(result);

Open in new window

That will output the contents of the result object to the developer console in the browser.  Verify that the returned values from your PHP script look correct.  Additionally, it looks like there could be a problem with your shipping field.  Is this fixed, regardless of how many of an item someone orders?  If it is, then you may not have an issue.  If you are trying to update it with your AJAX function, you will want to change the shipping input field to something similar to the amount field.
<input type="hidden" name="shipping" id="total-shipping" value="">

Open in new window

Avatar of crescue

ASKER

I wouldn't knpw what am I doing wrong, but I have created another "input type" field with a different name, just to see if it is populated outside the Paypal area, and it still doesn't populate with the amount (of course, I ve changed the variable name in ajax too)
Can you test it in your side to verify that it works as it should ?
I ve verified the console too, and the amounts pasted are correct. Can the "input type" be populated via programming ?
ASKER CERTIFIED SOLUTION
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crescue

ASKER

Tnx for all your help  Great asset to experts exchange forum