Jump to content

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
https://forums.phpfreaks.com/topic/307939-post-with-variable-as-field-name/
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>

 

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.