I have a form that I would like to send to saveConfig.php so that the data can be saved into a MySQL database. I would like to pass the selected form options, as well as the total.
<form id="configSave" action="" class="hide-submit">
<div class="btn-configure" id="configSave">
<span class="pcButtonText">
Save
</span>
</div>
<ul id="radio" class="input-list">
<li>
<input id="item-1" name="config-prod" value="1.00" type="radio" onchange="updateTotal();">
<label for="item-1">Item 1</label>
</li>
<li>
<input id="item-2" name="config-prod" value="2.00" type="radio" onchange="updateTotal();">
<label for="item-2">Item 2</label>
</li>
<li>
<input id="item-3" name="config-prod" value="3.00" type="radio" onchange="updateTotal();">
<label for="item-3">Item 3</label>
</li>
</ul>
<select id="plist" name="partlist" onchange="save();">
<option value="99.99">CPU 1</option>
<option value="123.00">CPU 2</option>
<option value="250.54">CPU 3</option>
</select>
</form>
<br>
Total: <input id="total" type="text">
<script>
$('#configSave').on('click', function () {
alert("ok");
$.ajax({
url: 'saveConfig.php',
type: "POST",
data: $('#configSave').serialize(),
dataType: "JSON",
contentType: 'application/json;charset=UTF-8',
success: function (data) {
alert('saved');
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
</script>
Within the PHP file, how can I access the form data so I can save it into the database? I get this error message:
SyntaxError: Unexpected token < in JSON at position 2
That error means the JSON string that is arriving at the PHP server is malformed. The reason it is malformed is because you cannot serialize a <div>
. The serialize method is designed to serialize form elements Encode a set of form elements as a string for submission..
Change:
data: $('#configSave').serialize(),
To:
data: $('#form').serialize(),