Sending multiple data parameters with jQuery AJAX
When constructing an AJAX request to pass multiple values to a server-side script, the syntax used in the data option is critical. In the scenario you are facing, the failure to receive the user ID on the check_code.php page stems from a specific string concatenation error within your JavaScript.
You defined the data payload as:
data: 'code='+code+'userid='+userid
This line relies on implicit string concatenation in JavaScript. While it works for the code variable, the way it constructs the final string creates a malformed form key-value pair. Specifically, if code is "ABC123" and userid is 505, this expression evaluates to:code=ABC123userid=505
Notice that there is no space or ampersand separating the first parameter from the second. Your PHP script expects the incoming data to look like standard form submission arguments where keys are separated by spaces or ampersands (e.g., code=ABC123 userid=505). Because the keys are fused together, your PHP backend likely interprets the entire string as a single variable named codeuserid, leaving the userid value completely empty.
To fix this, you must explicitly add a space or an ampersand between the parameters. The corrected line should be:
data: 'code=' + code + ' userid=' + userid
Alternatively, using an ampersand is often safer as it strictly mimics URL encoding:
data: 'code=' + code + '&userid=' + userid
Using jQuery's object notation for the data option is also a robust alternative that avoids string manipulation entirely. This approach automatically handles key separation and encoding, reducing the risk of syntax errors. You would define your request like this:
$.ajax({
type: "POST",
url: "<?php bloginfo('template_url'); ?>/profile/check_code.php",
data: {
code: code,
userid: userid
},
datatype: "html",
success: function(result) {
// Handle response
}
});
When the server receives this request using $_POST, PHP will correctly populate $_POST['code'] and $_POST['userid']. If you are using JSON instead of raw POST data, ensure your PHP endpoint is configured to accept JSON payloads via application/json and decode it using json_decode.
Another pitfall in the provided code involves how the form submission triggers the AJAX call. The onsubmit handler calls checkDB with values from specific DOM elements:
checkDB(document.sc_ajax.sc_voucher_code.value, <?php echo $user_id ?>);
If $user_id is empty in the PHP context or if the variable is not initialized before being echoed into the JavaScript, it could result in a undefined value being passed. Ensure that the PHP variable $user_id contains a valid integer or string before injecting it into the client-side script. If dynamic values are involved, consider passing them via data attributes on the form element rather than relying on direct global variables to maintain separation of concerns.
Finally, verify that your server-side script is actually inspecting $_POST and not $_GET. Since the request type is POST, accessing $_POST is the correct approach. If you switch the jQuery configuration to use contentType: 'application/json', remember to change the PHP handler to read from file_get_contents('php://input') or utilize a framework like Laravel that abstracts this complexity.