1

I want to submit some data which I attach to an input element. The server get's this element, with correct ID, but it has no value.

<form id="sub_form" method="post" action="acc_manage.lp"> 
    <input type="text" name="container" id="sub_inp" value=""> </input>
</form>

sub_inp receives its input from a specific event, which calls:

function execute_submit(){
    $("#sub_inp").val(JSON.stringify(foo));
    // .val() returns a stringified object 
    console.log( $("#sub_inp").val() )
    if ($("#sub_inp").val() != "") {
        $("#sub_form").submit();
  };

Value of the post request on server-side is this:

post={ container={} }, formdata={}, errtag={} }

Why is this and how can I fix it? I am using jQuery 2.1.3

jawo
  • 856
  • 1
  • 8
  • 24
  • an JS object, let's say { a:"b", c:"d" } – jawo Aug 05 '15 at 09:06
  • alert(foo) prints [object][object], however alert( JSON.stringify(foo)) prints what I've expectet, "{ a:'b', c:'d' } " in this example. – jawo Aug 05 '15 at 09:11
  • Where is the server-side code that is receiving the response? Also note that `input` elements do not require a `` tag. – Rory McCrossan Aug 05 '15 at 09:13
  • I havent written the serverside myself, but it's based on this: http://www.teratronik.org/core4/Core4Event/httpd/cgi.html – jawo Aug 05 '15 at 09:17
  • But what is the actual code you've used? We can't diagnose the problem without seeing what the cause is. – Rory McCrossan Aug 05 '15 at 09:18
  • The entire page is assambled in lua and given per .cgi . In this script, there's the line > if ( cgi.post ) then print( table.tostring( cgi )) end < this is where I've got the post value from. – jawo Aug 05 '15 at 09:26
  • Sadly, I can't provide the entire script. Do you have a specific assumption? Would the entire post message help? – jawo Aug 05 '15 at 09:27

1 Answers1

1

There might some issues/misunderstandings about how your form is getting submitted, and what happens there. I cannot tell for sure, because some of the important code is missing form your example...

If your form has an action attribute:

<form action="demo_form.asp">
   ...
</form>

On submit, send the form-data to a file named "demo_form.asp" (to process the input)

(w3schools: form_action)

If you want to run javascript code on submit use onsubmit attribute

<form onsubmit="myFunction()">
   ...
</form>

Execute a JavaScript when a form is submitted

(w3schools: onsubmit)

If you specify both you might get unexpected results on the server side.

Similar problem: HTML form action and onsubmit issues

EDIT

Based on your comment, after clarifying that your code javascript code is running, we have to assume that the row:

$("#sub_inp").val(JSON.stringify(foo));

is messing up the HTML value of the input, like mentioned in this answer: store return json value in input hidden field

Community
  • 1
  • 1
DDan
  • 8,068
  • 5
  • 33
  • 52
  • Informative. I've given sub_inp the default value "test" to check if there's some problem as you mentioned it. But the problem stays the same. The received value isn't {"test"} but simply {}. So the value get's altered before submition. – jawo Aug 05 '15 at 10:03
  • 1
    I see. Take a look at this too: http://stackoverflow.com/questions/3448831/store-return-json-value-in-input-hidden-field#18443279 – DDan Aug 05 '15 at 10:08
  • 1
    Can it be that `JSON.stringify(foo)` is messing the HTML value data up. – DDan Aug 05 '15 at 10:10
  • Thats it. Could you please add this to your answer, so others can see the solution quicker? – jawo Aug 05 '15 at 10:16
  • 1
    *If you specify both, your javascript won't run before submit* — False. The submit event handler is fired before the form is submitted. – Quentin Aug 05 '15 at 10:20
  • You're right, I tried. I took the phrase away. Thanks! – DDan Aug 05 '15 at 10:23
  • 1
    DDan, simply .replace(/"/g, "|") (| or any other digit that cannot occur in the object) should be an easier solution, depending on what the serverside wants to do with the data. In my case, the JSON object will be parsed to a lua table, therefore .replace is much easier. – jawo Aug 05 '15 at 10:25