简体   繁体   中英

How to pass a PHP array as parameter from onClick HTML button to JavaScript?

When I press onClick and call a JavaScript function I want to pass an array. I tried by passing it as parameter but it doesn't work. The array is the data of a row in a datatable, so when I click in Edit button of a specific row I want to take the values of that row only and pass it to JavaScript.

<button type="button" id="button_edit" onclick="edit_customer_request()" value="<?php echo $user; ?>" name="edit_customer">Edit</button>

and jQuery:

function edit_customer_request() {
    var button_edit = document.getElementById("button_edit");
}

If you want to assign the onclick in the html as you're doing, you can pass through arguments to the callback function:

 function edit_customer_request(element,arr) { console.log($(element).prop('type')); console.log(arr); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" id="button_edit" onclick="edit_customer_request(this, [1,2,3])" value="<?php echo $user; ?>" name="edit_customer">Edit</button>

They even have an example on w3schools documentation: https://www.w3schools.com/jsref/event_onclick.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM