2

So this is the piece of script which gives me problems

function creditcard(){

var finaltotalprice = document.getElementById('finaltotalprice').innerHTML;
var total = document.getElementById('sahakol').innerHTML;
var fullprice = $('#fullprice').html();
   var phpval = parseFloat($('#cart-products-total').html());
   var name = $('#c_name').val();
   var city = $('#c_city').val();
   var add = $('#c_add').val();
   var phone = $('#c_phone').val();
   var email = $('#c_email').val();
   if(name  == '' || city == '' || add == '' || phone == ''){
   alert('נא וודאו כי מילאתם את כל שדות המשתמש');
   return false;
   } else {

   $('<form action="payment.php" method="POST"/>')
   .html( '<textarea name="total">' + total + '</textarea><textarea name="finaltotalprice">'+ finaltotalprice + '</textarea><textarea name="fullprice">' + fullprice + '</textarea> <textarea name="name">' + name + '</textarea> <textarea name="city">' + city + '</textarea> <textarea name="add">' + add + '</textarea> <textarea name="phone">' + phone + '</textarea> <textarea name="email">' + email + '</textarea> <textarea name="phpval">' + phpval + '</textarea>' )

   [0].submit();


   }}

This piece works exellent in google chrome, but broken in IE and firefox. The script should POST all the var's in the script to 'payment.php' and redirect the user to 'payment.php', but it doesnt redirects the user in IE and in FF. Any ideas?

Selina
  • 31
  • 2
  • You can call the `.submit()` method on the jQuery object. – Pointy Sep 21 '15 at 14:47
  • The way you separated `[0]` to another line looks wrong to me. No idea if one of the other browsers might be choking on that. – Kevin B Sep 21 '15 at 14:49
  • @Pointy you mean $().html().submit(); ? – Selina Sep 21 '15 at 14:51
  • No, he means simply removing `[0]` – Kevin B Sep 21 '15 at 14:52
  • 2
    @KevinB it's definitely weird but I doubt the problem is that. It *might* be that Firefox and IE want the form element to be part of the DOM. – Pointy Sep 21 '15 at 14:52
  • @Pointy: That would be my guess, but I'd have to de-obfuscate the source to double-check. :-) – T.J. Crowder Sep 21 '15 at 14:53
  • tried to remove the [0] and nothing has been changed. I even tried to redirect the user to the 'payment.php' page but by doing so the form doesnt posts anything via jquery – Selina Sep 21 '15 at 14:54
  • Is this an auto-insert `;` issue? Have you tried removing all the whitespace in the last lines? – Shilly Sep 21 '15 at 14:59
  • Try adding `.css("display", "none").appendTo("body")` before `.submit()` - that's just a guess but I have vague memories of having this problem in the past. – Pointy Sep 21 '15 at 14:59
  • @Pointy I have no idea why - but it works!! can anyone give me an explanation why it works? – Selina Sep 21 '15 at 15:02
  • from @Pointy: *"It might be that Firefox and IE want the form element to be part of the DOM."* – Kevin B Sep 21 '15 at 15:05
  • 1
    Related http://stackoverflow.com/questions/5208224/firefox-wont-submit-a-form-created-by-javascript – Stryner Sep 21 '15 at 15:05

1 Answers1

1

As Pointy mentioned: I've needed to add the form to the DOM. by adding

.css("display", "none").appendTo("body") 

before .submit(), now it works flawless.

Selina
  • 31
  • 2