What I've done is:
HTML
<form>
<div id="textBox" contenteditable="true" name="textBox"><?php echo $storyText; ?>
</div>
<textarea id="hiddeninput" name="hiddeninput"></textarea>
<input type="submit" id="save" name="save" value="Submit"/>
</form>
Javascript
$('#save').click(function () {
var mysave = $('#textBox').html();
$('#hiddeninput').val(mysave);
$("form:first").submit();
$('#hiddeninput').append(mysave);
alert($('#hiddeninput').val());
});
So both the alert and the append display the correct information, but it won't save #hiddeninput as a php variable when I submit. Originally I had this as an hidden input method, but I'm trying to show that it won't post no matter what I do,
It is completely acceptable to use a DIV inside a <form> tag. If you look at the default CSS 2.1 stylesheet, div and p are both in the display: block category.
Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to create the registration form. Step 2: Now, we have to place the cursor at that point where we want to create a form between the starting and closing of <body> tag in the Html document.
<form> is what you use to create a form for people to fill in. Div is used to divide sections of the web page up.
Your code is working almost as it is.
But I'd rather use normal <input type="hidden">
and you don't need to trigger submit
for your form in your case just put the value in a hidden field.
Given your markup, with slight modifications
<form action="showrequest.php">
<div id="textBox" contenteditable="true" name="textBox" style="width:300px;height:100px;">
</div>
<textarea id="hiddeninput" name="hiddeninput"></textarea>
<input type="submit" id="save" name="save" value="Submit"/>
</form>
js
$(function(){
$('#save').click(function () {
var mysave = $('#textBox').html();
$('#hiddeninput').val(mysave);
});
});
var_dump($_REQUEST)
on php side gives
array(2) {
["hiddeninput"]=>
string(4) "test"
["save"]=>
string(6) "Submit"
}
Try binding the submit event instead of the click event. What might be happending is the form is submitting before the value of your textarea is set.
$('form').submit(function(){
var mysave = $('#textBox').html();
$('#hiddeninput').val(mysave);
});
I tested this example with method="get" and got the html from the div to show up in the URL.
I believe your form is being submitted before your js code has a chance to run. Since you're submitting manually from jQuery, try preventing the default event of the button (which also submits the form):
$('#save').click(function(e) {
e.preventDefault();
var mysave = $('#textBox').html();
$('#hiddeninput').val(mysave);
$("form:first").submit();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With