0

Step 1: On clicking a button, show a modal.

<button class="btn btn-sm fa fa-edit" onclick="updateJobModal('<?php echo $custContactName ?>','<?php echo $jobDesc ?>','<?php echo $detailedDesc ?>','<?php echo $poNumber ?>','<?php echo substr($dueDate, '0', '10') ?>','<?php echo $prevJobNumber ?>','<?php echo $totalSellPrice ?>','<?php echo $sellLevel ?>','<?php echo $brandType ?>','<?php echo $clientRef ?>')"> &nbsp;</button>

JavaScript:

const updateJobModal = (contact, jdesc, detailed, pono, duedate, prevjob, total, selllevel, brand, cliref) => {
  let recordInUse = <?php echo $recordInUse?>;

  if (recordInUse === 1) {
    swal('Error', 'Unable To Update! Job Is Currently Open In EPMS!!', 'warning');
  } else {
    console.log([contact, jdesc, detailed, pono, duedate, prevjob, total, selllevel, brand, cliref]);
    document.getElementById('jobdesc').value = jdesc;
    document.getElementById('contact').value = contact;
    document.getElementById('contact').innerText = contact;
    $('#ddesc').val(detailed);
    $('#ponumber').val(pono);
    $('#duedate').val(duedate);
    $('#prevno').val(prevjob);
    $('#totalprice').val(total);
    $('#selllevel').val(selllevel);
    $('#brand').val(brand);
    $('#clientref').val(cliref);
    $('#modal-form').modal('show');
  }
};

Now the problem is the field $detailedDesc has commas in it. Example:

Lot B--Box , Blank Tray , Top Insert, Mailing Label

It breaks the function and causes Uncaught SyntaxError: Invalid or unexpected token.

Function call causing the error:

updateJobModal('','Snack Pack Custom Box Mailing','Lot B--Box ,  Blank Tray ,  Top Insert,  Mailing Label
','Array','2018-07-31','','235315','20/30','Brand','Client Ref')

How do I get it to working correctly?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Are you sure it’s not the linebreak within the string in the function call? – Sebastian Simon Jan 21 '21 at 20:18
  • 2
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+php+escape+strings) of [Pass a PHP string to a JavaScript variable (and escape newlines)](https://stackoverflow.com/q/168214/4642212). – Sebastian Simon Jan 21 '21 at 20:19
  • Inline event handlers like `onclick` are [not recommended](https://stackoverflow.com/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](https://stackoverflow.com/a/43459991/4642212) way of registering events. Always [use `addEventListener`](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Inline_event_handlers_—_dont_use_these) or jQuery’s [`.on`](https://api.jquery.com/on/) instead. – Sebastian Simon Jan 21 '21 at 20:20

0 Answers0