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! Quote Link to comment https://forums.phpfreaks.com/topic/307939-post-with-variable-as-field-name/ 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> Quote Link to comment https://forums.phpfreaks.com/topic/307939-post-with-variable-as-field-name/#findComment-1562390 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... Quote Link to comment https://forums.phpfreaks.com/topic/307939-post-with-variable-as-field-name/#findComment-1562395 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; Quote Link to comment https://forums.phpfreaks.com/topic/307939-post-with-variable-as-field-name/#findComment-1562404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.