0

So, I'm working on figuring out how to post data from Jquery to PHP, And as much as I follow the examples I've found on the threads here, I keep getting an "Undefined Index Name" error.

My code thus far for the JQuery side is

<script src="jquery-1.11.1.min.js"></script>
</script>
<script>
$(document).ready(function(){
     $("#div2").text('Hey');
    $("#div1").load('testFile.txt');
    setInterval(function() {
    $.ajax({ url: 'script.php' });
    $("#div1").load('testFile.txt');}
    ,100);



  });
  function sub(){

     var msg = $("#name").val();

     $.post('chat.php',{'name':"1234"},function(){
$("#div2").load('chat.php');
});  
 };

</script>

The html forms and buttons I'm using

<div id="div1"></div>
<div id="div2">Um</div>

<form name="myForm" id="myForm" action="" method="POST">
<input type="text" name="name" id="name" size="30" value=""/>
</form>
<button id="submission" onclick="javascript:sub();">Errrr</button>

And the PHP side I'm going to

<?php
echo $_POST['name'];
 $myFile = "testFile.txt";
 $fh = fopen ($myFile, 'a+') or die("Cannot Open File");
 fwrite ($fh, $_POST['name']);
 fclose($fh);

?>

I'm about at a loss from where to do. All files are within the same folder and filenames are correct as far as I can find.

3
  • Why would you run a post and then load the same script seperately..? Commented Jul 9, 2014 at 0:55
  • That's me chasing my tail trying to trace why I keep getting that error. Commented Jul 9, 2014 at 0:56
  • what is this supposed to do? $.ajax({ url: 'script.php' });..nothing sent and no handler for anything returned. Commented Jul 9, 2014 at 2:08

1 Answer 1

1

Your issue is most likely that you're doing this:

$.post('chat.php',{'name':"1234"},function(){
    $("#div2").load('chat.php');
});  

You're effectively sending the data {'name': '1234'} to the file and then just loading the file? You'll get the error because you load() the file without sending it params.

Looking at the jQuery.post manual, you'd see that you can get a response with something like this:

$.post('chat.php',{'name':"1234"},function(data){
    console.log(data);
}); 

While you use something like echo or print to effectively "return" content to the ajax call.

<?php
echo $_POST['name'];
?>

Now check in your console and see if there is a response.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting the value returned in the console, but am getting no signs that the PHP script is properly running.
If you're getting the value you send with {'name': name} then the script should be running correctly...
@Draketh how is this going for you..?
Thanks to your help I was able to solve it, and feel a bit stupid for it, I had two conflicting scripts running, which was my main problem. Thank you so much for your help!
@Draketh Did this answer your question at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.