0

I want to pass a value from my jquery code to a php variable after a user inputs through a textbox, but I don't receive any value at all in the PHP side through POST.I'm using .change and .post, does anyone here knows how to do it properly? here's my code:

$(document).ready(function(){
 $("#packagename").change(function(){
    var packagename = $('#packagename').val();
    var url = '{{url}}'; //localhost/test/test.php
    $.ajax({
    type: 'post',
    url: url,
    dataType:html,
    data:{'val':packagename},   
    });
  });
});
3
  • 1
    api.jquery.com/jQuery.post Commented Aug 29, 2013 at 2:26
  • You can't alter PHP variables with JavaScript, since PHP is a server-side language and thereby totally unaware of anything happening in the browser. POST requests is handled on the page you are posting to, and does not affect the page you are at. Commented Aug 29, 2013 at 2:27
  • possible duplicate of How can I upload files asynchronously with jQuery? Commented Aug 29, 2013 at 2:56

4 Answers 4

1

try it

$(document).ready(function(){
 $("#packagename").change(function(){
    var packagename = $('#packagename').val();
    var url = '{{url}}'; //localhost/test/test.php
    $.ajax({
    type: 'post',
    url: url,
    dataType:text,
    data:{'val':packagename},  
    success:function(result){
alert(result);
} 
    });
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@NeilAntonio If it has a alert display, it mean your JS working, then what are you expecting?
0

The only problem that I can see offhand is that the html in dataType:html, needs to have quotes around it like this: dataType: 'html',. Otherwise your code will look for the variable html, not find it, and throw an error.

4 Comments

I already did, but still I don't receive any value in my php code: echo $_POST['val']
Does your javascript console show any errors? Do you get any response from the server?
What output do you see when you do a var_dump($_POST) instead of echo $_POST['val']?
That probably means that the script isn't outputting anything, so the script might not being called at all. Are you calling //localhost/test/test.php using that exact syntax? If so, that will look for a file on your local drive. Try changing to /localhost/test/test.php and check the output.
0

http://api.jquery.com/jQuery.post/

dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

change dataType: html, to dataType: 'html',

1 Comment

I already did, but still I don't receive any value in my php code: echo $_POST['val']
0

Have you tried just putting the url into the url field instead of whatever that object is you are trying to use?

I will assume you are not trying to ajax to the page you are currently on and expecting the php variable to display on the current page.

2 Comments

Im trying to ajax it to the current page...do you have any ideas on how I could properly handle it?
For updating the current page, you would just do that with javascript. For storing the value in a database for example, you would ajax it to another php page that is used to write to the database. You could do both to simulate updating the current page.

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.