phdphd Posted November 23, 2018 Share Posted November 23, 2018 Hi All, I have a $.post query with a field/value pair as an input parameter $.post('query.php',{field_name:value},… This code is inside a function that is triggered by an onkeyup event onkeyup="myFunctionV()" As such it works. Now, is it possible to pass the "field_name" part of the query as a parameter ? The following triggers the function but fails with a "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data" error. onkeyup="myFunctionV('field_name')" function myFunctionV(hello) { … $.post('query.php',{hello:value},... Thanks! Link to comment Share on other sites More sharing options...
Barand Posted November 23, 2018 Share Posted November 23, 2018 Is this what you are trying to do <?php // OUTPUT THE VALUE POSTED BY AJAX REQUEST if (isset($_POST['fieldname'])) { exit("\"{$_POST['fieldname']}\" was sent OK"); // send ajax response } ?> <html> <head> <title>sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $(".mybutton").click( function() { $.post ( "", // send to self {"fieldname" : $(this).val() }, function(resp) { alert(resp) }, "TEXT" ) }) }) </script> </head> <body> <button class='mybutton' value='field A'>Button 1</button> <button class='mybutton' value='field B'>Button 2</button> <button class='mybutton' value='field C'>Button 3</button> </body> </html> Link to comment Share on other sites More sharing options...
phdphd Posted November 23, 2018 Author Share Posted November 23, 2018 Not really. But anyway thanks for the time spent on it. I think I was misleading you (and myself too) by using "field name". What is sent is not actually a field/value pair, but a value that will go in the WHERE clause of a MySQL query where fields are already set. I realized I need no JS parameter for this in fact... Link to comment Share on other sites More sharing options...
requinix Posted November 23, 2018 Share Posted November 23, 2018 The syntax you want is { [hello]: value } but it's new to ES6 and not all browsers support it. So you should continue with the old syntax of var data = {}; data[hello] = value; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.