2

When ever I pass a value from a form using submit to a php file using load, $.get or post and try to receive data or load it in general as shown below, I just get ruturned a null or undefined value. Anyone know what im doing wrong ?

iNDEX.HTML `

<script type = "text/javascript">
    $(document).ready(function(){

        $("#NewOrgForm").submit(function(){
            var name = $("#companyname").val();  
            $(".content").load("NewEntry.php",{Name: name});
        });
    });
</script>


<form id="NewOrgForm">
<p> Name: <input type="text" id="companyname" value="" /> </p>
<p> <input type="submit" value="Submit New Entry" id="submit" /> </p>
</form>

NEWENTRY.PHP

<?php

$companyname = $_POST['Name'];
echo $companyname;

?>

`

1
  • Check if var_dump($_REQUEST) doesn't give some interesting facts away. Commented Jul 25, 2010 at 17:36

3 Answers 3

1

From some of the comments you posted it looks to me like you are incorrectly collecting data from the form. Have you tried making an alert to see if the name variable is actually set? Also try using firebug or something to see the exact URL you're loading.

Perhaps try using a directly passed event reference to the function and fetch the fields from there instead of using an absolute selector. Might help.

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

2 Comments

Yes i have made an alert to see if the name variable is actually set and it does output what I typed in.
Resolved: Just resorted to good old method and post, a bit dissapointed by this but o well. If anyone finds the time and manages to get this working, please feel free to post your suggestions on here, thanks for all the help!
1

Is there actually an element in your HTML that has the class "content"?

Try making use of the callback function so that you are aware that the load actually ran:

$('#result').load('ajax/test.html', function() {
  alert('Load was performed.');
});

The above code is taken from the jquery site (http://api.jquery.com/load/)

UPDATE: The more I look at this, the more I think that you are probably better going w/.post (http://api.jquery.com/jQuery.post/)

UPDATE2: What happens if you try this:

$(document).ready(function(){
  runOnLoad('content');
});

function runOnLoad(className){

  var name="peter";
  var ajaxData = {"Name": name};
  var url="NewEntry.php";
  var contentToReturn="";

  $.ajax({
     type: "POST",
     url: url,
     data: ajaxData,
     datatype: "html",
     success: function(html) {
       contentToReturn="Success! It returned "+html+"<br>";
       $('div.'+className).html(contentToReturn);
     },
     error: function(html) {
       contentToReturn="Failure! It returned "+html+"<br>";
       $('div.'+className).html(contentToReturn);
     }

  });
  return;
}

7 Comments

Yes there is an element with the class "content", it is a div class. all that happends is the page refreshes and it outputs Load was performed.
The strange thing is if I run the document like this, $(document).ready(function(){ var name="peter"; $(".content").load("NewEntry.php",{Name: name}); It works just fine!
Even if i use $.post("NewEntry.php",{Name: name},function(data){ alert("data received is " + data); }); I still receive null data back. If you would test this out yourself, it would be helpful.
What happens if you do something like in my "UPDATE2" above?
Well the code you posted above doesnt do anything when I run it. I could easily do the same thing with <script type = "text/javascript"> $(document).ready(function(){ alert("Looking for Peter"); var name = "Peter"; $.post("NewEntry2.php",{Name: name},function(data) { alert(data); },"json"); }); </script> which works but i dont think that is the problem. Im guessing the problem is reading the data from the form / posting the form data.
|
0

If you're just doing a POST, it should look something like:

<script type = "text/javascript">
    $(document).ready(function(){

        $("#NewOrgForm").attr({
            "method": "POST",
            "action": "NewEntry.php"
            }).submit();
    });
</script>

although why even use jquery for this, then...just set method and action attribs on your form element...

1 Comment

hmm yes I might use method and action attribs. Its strange because on my other document I use onclick(data) from a submit input and pass the 1 set of data to a function and do the exact same code above and it works. Not sure why extracting it using jquery and passing along does not work.

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.