I’ve got a reference field to the ‘cmdb_ci’ table, that onChange should then perform various populations and field manipulations. I’m having difficulties with passing multiple values back to the client script.
Client Script –
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
console.log('newValue: ' + newValue); // returns sys_id as expected
// Call the Ajax include
var ga = new GlideAjax('u_NewMSSQLInstanceAjax');
ga.addParam('sysparm_name', 'validateSQLHost');
ga.addParam('sysparm_host_id', newValue);
ga.getXMLAnswer(function(clusterName){
var answer = clusterName.responseXML.documentElement.getAttribute("clusterName");
console.log('answer: ' + answer); // Getting an error here - "Uncaught TypeError: Cannot read property 'documentElement' of undefined"
//answer = answer.evalJSON(); //Transform the JSON string to an object
// If there is a relationship
if (answer.parent) {
// Hide any existing field messages
g_form.hideFieldMsg('mssql_host', true);
// Update the value
g_form.setValue('mssql_host', answer.parent);
// Display a message to the user so they know why it was updated
var msg = 'The server you selected is a cluster node. The selection has been updated to show the cluster name instead of the node.';
if (typeof spModal != 'undefined') {
spModal.alert(msg);
}
else {
alert(msg);
}
}
// If host environment is not blank
if (answer.env) {
// Set the environment class and read-only
g_form.setValue('mssql_environment_class', answer.env);
g_form.setReadOnly('mssql_environment_class', true);
}
});
}
Script Include –
var u_NewMSSQLInstanceAjax = Class.create();
u_NewMSSQLInstanceAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateSQLHost: function () {
// Declare result
var result = {};
result.parent = "";
result.env = "";
// Get parameters
var hostID = this.getParameter('sysparm_host_id');
// Query for a parent cluster record
var grCiRel = new GlideRecord('cmdb_rel_ci');
grCiRel.addQuery('type', // Type of 'Cluster Nodes::Cluster Node for'.
grCiRel.addQuery('child', hostID);
grCiRel.query();
// If we found a parent cluster
if (grCiRel.next()) {
// Update the result to the parent sys_id
result.parent = grCiRel.getValue('parent');
}
// Query for the environment class of the host
var grCI = new GlideRecord('cmdb_ci');
grCI.get('hostID');
result.env = grCI.getValue('u_env_class');
var json = new JSON();
var data = json.encode(result); // JSON formatted string
// Return the result
return data;
},
type: 'u_NewMSSQLInstanceAjax'
});
How to post multiple parameters to the server?
$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "../Home/GetSomeData",
"dataType":"JSON",
"type": "POST",
"data": function (d) {
return $.extend({}, d, {
"objectParameter": someObject,
"booleanParameter": booleanValue,
"arrayParmeter": someArray,
});
}
},
Receiving Multiple Parameters into PHP from jQuery Ajax Call
$("#submissionform form").submit(function(e){
var origEmail = $('#go').val();
var confirmEmail = $("#confirmemail").val();
var name = $("#name").val();
var age = $("#age").val();
var country = $("#country").val();
$.ajax({
type: "POST",
dataType: 'json',
cache: false,
url: "Scripts/confirmform.php",
data: { origEmail: origEmail,
confirmEmail: confirmEmail,
name: name,
age: age,
country: country },
success: function(data){
testSecondResults(data);
}
});
});
});
I want to be able to receive those parameters into the PHP script for all the values into the following PHP script (filename: confirmform.php).
<?php
$instance = new CheckForm;
$instance -> checkSubmission();
class CheckForm
{
public function checkSubmission()
{
$response = array("validation" => " ", "message" => " ");
if ($_POST['country'] != "Select Country")
{
if (isset($_POST['confirmemail']) && isset($_POST['name']))
{
$origEmail = $_GET['go'];
$confirmEmail = $_POST['confirmemail'];
if ($origEmail == $confirmEmail)
{
$name = htmlspecialchars($_POST['name']);
$ageRange = $_POST['age'];
$country = $_POST['country'];
require_once('databasewriter.php');
$dbWriter = new DatabaseWriter;
$dbWriter -> writeUserToDatabase($confirmEmail, $name, $ageRange, $country, $category);
$response = array("validation" => "pass", "message" => 'Thanks for joining the e-mail list, ' . $name . ', under the e-mail address, ' . $confirmEmail . '.');
} else {
$response = array("validation" => "fail", "message" => 'E-mail addresses don\\'t match.');
die();
}
} else {
if (!isset($_POST['confirmemail'])){
$response = array("validation" => "fail", "message" => 'Confirmation e-mail not entered.');
} elseif (!isset($_POST['name'])) {
$response = array("validation" => "fail", "message" => 'Please enter a name.');
}
}
} else {
$response = array("validation" => "fail", "message" => 'Please select a country.');
}
echo json_encode($response);
}
}
?>