Jump to content

$.post with variable as field name


phdphd

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.