Get value of div and other elements on button click

1.2k Views Asked by At

I have a HTML form which consists of divs and ul element. I want to get all the data on a button click. I know how to get when there is input type element involved but how to deal with only div's and list elements?

html

  <div class="boxInner">

    <form id="my_form" method="POST">
    <div class="inner-box-header">
      %s
    </div>
    <div class="inner-sub-header">
      %s
    </div>

    <div class="inner-box-footer">
      <ul>
        <li>
            <img src="images/assets_icon_location.png" style="height:13px;width:10px;float:left;margin-right:3px;" alt="">
            <span class="image-side-text">%s</span></li>
            <li id="ip-location">&#9899<span>%s - %s</span></li>
      </ul>
    </div>

    <div class="titleBox">
      <input type="submit" value="Click" name="ws_butt" />
    </div>
    </form>
    </div>

python script

request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
d = parse_qs(request_body)

How to get all the elements in the request body and retrieve while parsing? Is there any other way to tackle this?

3

There are 3 best solutions below

0
On

I thought of putting input type="hidden" with every value I need and that will do the job for me.

 <form id="my_form" method="POST">
 <input type="hidden" name="name" value="%s">
1
On

You cannot.

The body of your POST request will only contain values from input elements, and it does not contain other information.

If you must do this, you have two basic options:

  1. Create actual form fields (best option); and then style them accordingly.
  2. Create hidden form fields, and then using javascript add the value of the respected divs to the form field's value before the form is submitted.

As you are using POST in your example, make sure you have a mechanism to stop CSRF. The easiest way to do this would be to use a Python framework.

0
On

Form data are only sent with HTML input elements.

Use hidden fields for each of Div, Li etc. Then use the load event of window to capture their values into the hidden fields.

<head>
$(window).load(function(){
var a = $('#div1').text();
$('#div1Value').val(a);
});
</head>

<body>
<form method='post'>
<input type='hidden' id='div1Value'>
<div id='div1'>Some text</div>
<input type='submit' value='Submit'>
</form>
</body>

Then use this JQuery code :