i am trying to grab the hidden input value from a form i have. The form passes all the elements properly EXCEPT for the HIDDEN elements. I know they are declared because if I choose "view source" on the page it shows me the correct values (basically the user credentials).
<form name="addExperienceForm" id="addExperienceForm" style="display:none;">
Title:<input type="text" name="title" id="title" />
From:<input type="text" name="startDate" id="startDate" />
To:<input type="text" name="endDate" id="endDate" />
Description:<textarea type="message" name="description" id="description"></textarea>
<input type="button" value="Submit" onclick="addUserExp()"/>
<input type="hidden" value="<?=$_SESSION['userID']?>" id="userID" />
<input type="hidden" value="<?=$_SESSION['email']?>" id="email" />
function addUserExp(){
//get info
var title = document.getElementById('title').value;
var startDate = document.getElementById('startDate').value;
var endDate = document.getElementById('endDate').value;
var userID = document.getElementById('userID').value;
var email = document.getElementById('email').value;
var description = document.getElementById('description').value;
//construct POST string with name value pair
var str = "title="+title+"start="+startDate+"end="+endDate+"userID="+userID+"email="+email+"desc="+description;
//establish XMLHTTP object
var req = getXMLHTTP();
if(req){
req.onreadystatechange = function(){
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('addNewExp').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
}
req.open("post", "addExperience.php", true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(str);
}
PHP
<?php
$title = $_POST['title'];
$startDate = $_POST['start'];
$endDate = $_POST['end'];
$userID = $_POST['userID'];
$email = $_POST['email'];
$description = $_POST['desc'];
$sql = "INSERT INTO `user_experience`(`user_id`, `email`, `experience_title`, `experience_desc`, `start_date`, `end_date`)
VALUES ('$userID', '$email', '$title', '$startDate', '$endDate', '$description')";
echo $sql;
?>
I am echoing out the MYSQL statement so you can see that the fields are not being populated. The output of the above produces the following when data is entered into the form:
INSERT INTO `user_experience`(`user_id`, `email`, `experience_title`, `experience_desc`, `start_date`, `end_date`) VALUES ('', '', 'test', 'Feb-2013', 'May-2013', 'test')
.value? Assuming the userID and email are already stored in the session and the script that processes the AJAX is in the same PHP instance, couldn't you just use$_SESSIONin the script that processes the AJAX instead of passing them in the query? +1 because it's not a bad question.