开发者

Jquery - How to save entire form div upon post?

开发者 https://www.devze.com 2023-03-05 11:07 出处:网络
I have a form with a lot of input text and check boxes. User are allowed to edit it after post submission. The data from the form are stored in the database. For me to allow the user to edit, I would

I have a form with a lot of input text and check boxes. User are allowed to edit it after post submission. The data from the form are stored in the database. For me to allow the user to edit, I would have to retrieve the form value from the database and populate the form.

There are at least 30 inputs and if I use PHP to compare each and everyone, the code is very unclean and I am sure there is another better way to do it.

I was thinking of storing the entire form's html in the database and reload it back when user hits the edit function.

Question 1 How can I save the entire div#save-div's html upon submission? How do I access $('#sav-div').html in the php post-submit page?

Example Code

<form type="submit" <form  name="post_item" id="post_item" method="POST" action="post_form开发者_Go百科.php">
    <div id="save-div">
       <input type="text" id="1" />
       .
       .
       .
       //many input text and check boxes
    </div>
</form>

Question 2 Do I un-escape/escape the html code when I retrieved it from database when my PHP page loads? Can anyone write a simple examples?


Submitting the HTML sounds like a bad idea.

What you can do though is to use a bit of javascript on the client side and build yourself a JSON object which you post instead of the form fields, using jQuery instance.

There is almost certainly a nice JSON parser for PHP which you can use to parse the JSON into the object model you have in PHP.

Here is an interesting SO question that explains how to "serialize" the entire form into JSON: Convert form data to JavaScript object with jQuery


Not a good idea to store the entire html in the database. Why don't you just include the form? When user clicks edit, retrieve the relevant input values and echo them into the form.

You can also use AJAX to do this, but would have to be more specific as want you want to accomplish.

On question 2, you don't have to do anything when retrieving from the database. But inserting into database you should escape.

0

精彩评论

暂无评论...
验证码 换一张
取 消