assign javascript variable to hidden form field[SOLVED]

jwagnerbds 30 Sep, 2009
Here is my html code
<?php 
$username =& JFactory::getUser();
echo $username->get('name') ; 
?>

<div class="form_item">
  <div class="form_element cf_text"> <span class="cf_text">Fill out the following form to register your item.</span> </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Make</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter Make of item you are entering!" id="text_1" name="make" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Model</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter the Model of item!" id="text_2" name="model" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Serial</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter the Serial of item!" id="text_3" name="serial" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Promo</label>
    <input maxlength="150" size="30" id="promo" name="promo" type="text" />


  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">SalesCode</label>
    <input maxlength="150" size="30" id="text_5" name="salescode" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Register Item" name="button_5" type="submit"/>
  </div>
  <div class="cfclear"> </div>
</div>

<input value="<?php 
$user =& JFactory::getUser();
echo $user->get('id') ; 
?>" id="hidden_6" name="getuserid" type="hidden" />

<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="caughtbynumbers@live.com" />
<input type="hidden" name="lc" value="US" />
<input type="hidden" name="item_name" value="Caught By Numbers Registration Fee" />
  

<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="button_subtype" value="services" />
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted" />


I have some javascript that gets the value of the promo textbox. I know the javascript is working because I can do an alert and it shows the value of what I type in the promo text field.

What I can't figure out is how to assign the value of that variable to the hidden field for amount.

I have tried doing <input type="hidden" name="amount" value="' + myvar +'" />, but it doesn't work.

I have also tried <input type="hidden" name="amount" value="" />, and in my javascript I write document.ChronoContact_ItemRegistration.amount.value = myvar;

Neither of them work. Does anyone know how to assign the value of one of my text fields to a hidden textfield?

Thanks in advance
GreyHead 30 Sep, 2009
Hi jwagnerbds,

Assumint (a) that MooTools is loaded (it probably is) and (b)that you have defined myvar in your script. Then I'd add an id to the hidden input id='amount' and use
$('amount').value = myvar;

Bob

PS If you use the Redirect plugin to send the info to PayPAl then you do not need to add all the fixed info into the hidden fields, you can add all that in the plugin.
jwagnerbds 30 Sep, 2009
Mootools is loaded. I have some of the fields required and it works. I also have my variable defined.

Sorry for being dense, but where do you want me to put the $ ('amount').value = promo;

Is it supposed to go in my javascript, or as value="$ ('amount').value = promo;" in my input box?

Thanks!
GreyHead 30 Sep, 2009
Hi jwagnerbds,

In the script.

Bob
jwagnerbds 30 Sep, 2009
It still isn't doing the trick for me

Here is my javascript
function checkForm()
{
if(ChronoContact_ItemRegistration.promo.value == "001" )  {
 var promo = '1.00';
alert(promo);

 $('amount').value = promo;
  }
}


My Form Tag Attachment is onSubmit="return checkForm(ChronoContact_ItemRegistration)"

in my html I have <input type="hidden" name="amount" id="amount" value="" />

Do I need to do a getElementID or something. How does the $('amount').value = promo; know what it is referencing?

Thanks again!
GreyHead 30 Sep, 2009
Hi jwagnerbds,

$('amount') is the Mootools short-hand for getElementByID

Is the function running onSubmit?

What do you see in the Debug info for the $_POST array?

Bob
jwagnerbds 30 Sep, 2009
Sorry for the late reply, I had to step out of the office.

Yes the function is running on submit.

Here is my debug.

Form passed first SPAM check OK
Form passed the submissions limit (if enabled) OK
Form passed the Image verification (if enabled) OK
Form passed the server side validation (if enabled) OK
$_POST Array: Array ( [make] => DeWalt [model] => Nailer [serial] => 653333211 [promo] => 001 [salescode] => [button_5] => Register Item [getuserid] => 62 [cmd] => _xclick [business] => [email]caughtbynumbers@live.com[/email] [lc] => US [item_name] => Caught By Numbers Registration Fee [amount] => 1.00 [currency_code] => USD [button_subtype] => services [bn] => PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted [3c54b9405062a16f3e388e61d9f111b3] => 1 [1cf1] => 33b96a00c714c4a4732eba5656dfc102 [chronoformname] => ItemRegistration )
$_FILES Array: Array ( )
Redirect URL set
Form passed the plugins step (if enabled) OK
An email has been SENT successfully from (Caught By Numbers)joel_wagner@caughtbynumbers.com to [email]joel_wagner@caughtbynumbers.com[/email]
Debug End
Redirect link set, click to test:
https://www.paypal.com/cgi-bin/webscr?make=DeWalt&model=Nailer&serial=653333211&promo=001&salescode=&getuserid=62&cmd=_xclick&business=caughtbynumbers%40live.com&lc=US&item_name=Caught+By+Numbers+Registration+Fee¤cy_code=USD&button_subtype=services&bn=PP-BuyNowBF%3Abtn_buynowCC_LG.gif%3ANonHosted
GreyHead 30 Sep, 2009
Hi jwagnerbds

And you have [amount] => 1.00 so I guess it's working OK??

Bob
jwagnerbds 30 Sep, 2009
When it redirects to PayPal, Paypal opens up and says

"Please provide an amount and click Update Totals."

So while the amount has a value of 1.00 it still isn't picking up that value unless I hardcode it.

I don't know much about PayPal, so this maybe crazy, but could PayPal be seeing the 1.00 as a string instead of numerical. So it doesn't know what to do with it? Not sure how to change the string to an number.

Thanks
GreyHead 30 Sep, 2009
Hi jwagnerbds,

Well there is no amount in the URL so the PayPal message is correct. But that's a different question . . .

If you use the Redirect plugin to send the info to PayPAl then you do not need to add all the fixed info into the hidden fields, you can add all that in the plugin.

You can also add the amount field there.

Bob
jwagnerbds 30 Sep, 2009
Thanks I will try that! Appreciate your help.

I will let you know if it works.
jwagnerbds 06 Oct, 2009
I know it took me forever to reply to this, but I haven't had a chance to work on the website until today.

I got the code you gave me to work, by replacing the $('amount').value with ChronoContact_ItemRegistration.amount.value.

Not sure why the shorthand wasn't working, but at least it is now.

Many thanks Greyhead for steering me in the right direction.

Joel
This topic is locked and no more replies can be posted.