I am fairly new to WordPress and am having some issues that I can’t solve on my own.
My goal is to update the session variable with the input the user entered in a textbox.
I then want to store the updated session variable to a text file.
I am using jquery and ajax. My code is not working. Any help would be great. Thanks.
I will do my best to explain my code.
I will be happy to add additional information if I am not clear.
The form contains the following code:
<input type="text" id="question_in" />
<input type="button" id="btn" value="Add" />
My wordpress header.php contains the following:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
jQuery('#btn').click(function() {
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
updateSession();
});
function updateSession(){
alert ("in updateSession!"); //for testing this displays
var new_question = $("#question_in").val(); alert ("input is "+new_question); // for testing this works
// This does the ajax request
$.ajax({
type:"POST",
url: "write-test.php",
data: "question=" + new_question,
success:function(){
alert ("in success!"); //this displays
},
error: function(errorThrown){
alert ("There is an error!");
}
});
}
</script>
Here is the code for write-test.php
<?php
session_start();
if(isset($_POST['question'])) {
$theQuestion = $_POST['question'];
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $theQuestion);
fclose($myfile);
?>
1 comment