I'm sorry if this is a duplication question, but I have searched and searched and can't seem to find any answers. I'm about to go insane with this problem...
I am making a Web Service form using REST API/JSON, and have everything under way except for an array of checkboxes. If the values go into the xmlhttp open request, there are double quotes {checkboxvar:"["Option One", "Option 2"]"} around the array turning it into a string. It throws a casting string error b/c the API expects an array.
I'm lost on how to do this.. Thank you so much to whomever can help me with this!
I have an HTML form with some checkboxes:
... yada yada yada ....
<div id="multipleSelect">
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</div>
... yada yada yada ....
<button id="createSubmit" onclick="createContact()">Create</button>
With a button that links to this Javascript function:
function createContact(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = JSON.parse(xmlhttp.responseText);
if(response['status'] == "ok")
{
$('#createContact').fadeOut(800);
}
else if(response['status'] == "error")
{
alert(response['message']);
}
else
{
alert("Something REALLY went wrong. And to be honest I don't know what yet.");
}
}
};
xmlhttp.open("GET", "php/create.php?module=Contacts&data=First%20Name:"
+ document.getElementById('first_name').value +
"|Middle%20Name:" + document.getElementById('middle_name').value +
"|Last%20Name:" + document.getElementById('last_name').value, true);
xmlhttp.send();
}
Which then sends the request to this PHP file:
<?php
$module = $_GET['module'];
$dataurl = urldecode($_GET['data']);
$fields = explode("|",$dataurl);
$data = array();
for($i=0;$i<sizeof($fields);$i++)
{
$field = explode(":",$fields[$i]);
$data[$field[0]]=$field[1];
}
$data = '{"createFields": '.json_encode($data).',"returnFields":["Entity ID"]}';
if($module)
{
$call = $host."modules/".$module."/";
}
else
{
$call = $host."modules/"."noModule"."/";
}
echo $data;
$curl = curl_init($call);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Connection: Keep-Alive'));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_USERPWD, $userName.":".$passKey);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
echo $curl_response;
?>
Aucun commentaire:
Enregistrer un commentaire