3

I am having trouble passing AJAX data to PHP. I am experienced with PHP but new to JavaScript.

HTML / JavaScript

<input type="text" id="commodity_code"><button id="button"> = </button>

<script id="source" language="javascript" type="text/javascript">

$('#button').click(function()
{
  var commodity_code = $('#commodity_code').val();

  $.ajax({                                      
  url: 'get_code.php',  
  data: "commodity_code: commodity_code",
  dataType: 'json',
  success:function(data) {
        var commodity_desc = data[0];
        alert(commodity_desc);
    }  
  });
}); 

</script>

PHP

$commodity_code = $_POST['commodity_code'];

$result = mysql_query("SELECT description FROM oc_commodity_codes WHERE code = '$commodity_code'");
$array = mysql_fetch_row($result);
echo json_encode($array);

I know the general AJAX fetch and PHP code is working as I can manually create the $commodity_code variable and the script works fine. I think my issue lies somewhere in passing the AJAX data to my PHP script.

2
  • 2
    the mysql_ database library that you're using is end-of-life. It was deprecated several years ago and removed entirely in PHP 7 due to security concerns, including (but not limited to) its inability to support parameterised queries to better protect against SQL Injection attacks. As it is, you are running unsupported code and your system is very vulnerable to injection attacks. If this is new code, you should absolutely not be using mysql_, if it's old code you should plan to upgrade a.s.a.p. You should switch to mysqli_ or PDO and start using parameterised prepared statements. Commented Jun 13, 2017 at 8:52
  • Thank you, this is old code and will be changed to mysqli Commented Jun 13, 2017 at 8:56

2 Answers 2

6

You forgot to add the method: 'POST' in your AJAX Call. And you have some issues with your call. Check below:

$.ajax({                                      
  url: 'get_code.php',
  method: "POST",                         // Change here.
  data: {commodity_code: commodity_code}, // Change here.
  dataType: 'json',                       
  success:function(data) {
        var commodity_desc = data[0];
        alert(commodity_desc);
  }  
});

Or to make it simple, use the shorthand function:

$.post('get_code.php', {commodity_code: commodity_code}, function(data) {
  var commodity_desc = data[0];
  alert(commodity_desc);
});
Sign up to request clarification or add additional context in comments.

5 Comments

clarification : why is it a risky move to add dataType:'json' we can see in his php he is returning an array being json_encode ?
justification: @guradio Personal experience. I don't have a valid explanation for it. Many a times, removing that line worked for me. :)
@Jana What you just commented makes no sense. Did you understand the question and my answer? Why did you comment this?
Thank you, this is great. But I do not understand how the json DataType is risky?
@PraveenKumar i see happy coding mate :)
0

error in this line data: "commodity_code: commodity_code", .. you can simple pass the commodity_code variable..

$.ajax({                                      
  url: 'get_code.php',
  method: "POST",                         
  data: commodity_code, 
  dataType: 'json',                      
  success:function(data) {
        var commodity_desc = data[0];
        alert(commodity_desc);
  }  
});

Comments

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.