How can I send contents of a div as a POST parameter? - php

I have this Code:
<div contenteditable="true"><p><?php echo $row[1]; ?></p></div>
Can I take the contents of the div and send them as a POST parameter in order to use them in the PHP. It would be good if I can use: onchange="this.form.submit()".
Thanks!

It is not possible to post contents of div tags, as this is only possible on form elements. The workaround for this would be to use some Javascript that populates a hidden field when a form is submitted, and the hidden field is posted instead.
Observe the following HTML. See that there is an onsubmit event attached to the form element. What we're saying to the browser here is when the form is submitted, first call the Javascript function process, and only submit if said function returns true:
<form method="post" action="process.php" onsubmit="javascript: return process();">
<input type="hidden" id="hidden" name="content" vlaue="<?php echo $row[1] ?>">
<div contenteditable="true" id="content"><p><?php echo $row[1] ?></p></div>
<button type="submit">Post</button>
</form>
This would be your Javascript. What you're doing is getting the innerHTML of the element with the id content and assigning it to the value of the element with the id hidden and return true so the form can be successfully submitted:
<script>
function process() {
document.getElementById("hidden").value = document.getElementById("content").innerHTML;
return true;
}
</script>
And in the process.php file, just output the posted content:
var_dump("Posted content: " . $_POST['content']);
Hope this helps!

Related

Form Hidden Field Prints out Content to Webpage

I have the following form code:
<form action="pdf.php" method="POST" id="pdfform">
<input type="hidden" name="htmlcontent" value="<?php echo $content ?>" >
<li>Download as PDF</li>
</form>
However, what i realise is that the hidden field prints out the content to the HTML page as well, and there are some extra " and > which should not be there.
What i think could be the issue is because the role of the form is to send the html data to a PHP script to convert it to a PDF, the variable $content contains html code, for example: <p>Test 3</p><p><img alt="Cancer" src="http://breakthroughs.cityofhope.org/wp-content/uploads/2013/02/lung-cancer.jpg" style="height:375px; width:500px" /></p>
This could be one of the causes of the issue and the html prints out the extra "> at the end of the value inside the hidden form as well.
Anyone could find out the reason?
Not sure if this is what you want, but have you considered using jquery to set the hidden field? for example (after ensuring you have no single quotes in your $content variable):
<script type='text/javascript'>
$('[name=htmlcontent]').val('<?php echo str_replace("'","`",$content) ?>');
<script>

How submit a form from JQuery function without ID

I have this form, but this form inside PHP foreach function that gives the value from database, so I can't give the form id because redundancy will happened. And when click on any star in stars rating it will do as a submit, go to function so in the function I want to submit the form without id how?
<form class="watching-us-reating-form" name="swatching-us-reating-form" action=" method="get">
<input type="hidden" name="watchlist_id" value="<?php echo $fundAndUserWatchingThem['contactId']?>">
<!-- This hidden field used to save the value of the rating before submit -->
<input type="text" name="priority" class="watching-us-reating-value">
<!-- This DIV to contain the stars rating -->
<div class="watching-us-rating-div" data-score="<?php echo $flag; ?>" data-number="<?php echo $numberOfStars; ?>"></div>
</form>
You can use closest() to get the form element related to the clicked star rating:
$('.watching-us-rating-div').click(function() {
$(this).closest('form').submit();
});
Also, your form tag is missing an action - is that an error in your example?

jQuery change div to HTML5 contenteditable and pass to php

Current I have the following div
<div class="tag-header">
<?php echo $row->name; ?>
</div>
And I add the contenteditable to the div with .attr
$('.tag-header').attr("contenteditable", "true");
But my problem is how to pass the value to php and post to database?
*NOTE: I'm not ready to use ajax just normal php submit.
Added a demo here
Update html to
<form>
<div class="tag-header">
<?php echo $row->name; ?>
</div>
<input type="hidden" id="tagval" />
</form>
Then use the focusout event to bind
$(".tag-header").focusout(function() {
$("#tagval").val($(".tag-header").text());
});
Submit the form to get the value in PHP.

Form is not navigating to another page

<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >
<p>
<input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" />
</p>
</form>
<script type="text/javascript">
function myClickHandler(){
if(validation()){
showConfirm();
}
}
</script>
<?php
session_start();
$outputDetails = "";
$outputDetails .= "<table id='sessionDetails' border='1'>
<tr>
<th>Number of Sessions:</th>
<th>{$_POST['sessionNum']}</th>
</tr>";
$outputDetails .= "</table>";
echo $outputDetails;
?>
Above is the code for my form. What I am trying to do is that if the user submits the form, then it will go back to its own page. But if the "SessionNum" equals '1', then instead of posting the form to itself, it should post the form or in other words navigate to the "session_marks.php' page but it is not idng this, if sessionNum equals 1 then it still submits form or navigate back to its own page, what am I doing wrong?
Also lets say it displays a number for the sessionNum and then I submit the form and it submits the form back to itself, the number disappears, how do I keep the number displayed when submitting the form to itself?
Thanks
Where is the conditional logic to change the target of the form post? All I see in the form tag is this:
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
This will always set the form's action to be the current PHP file, not any other PHP file. If you want to conditionally post to a different file, you'll need to add conditional logic in there. Something like this (though there may be better ways to do it, keep in mind that I'm very out of practice with PHP):
action="<?php $_POST['sessionNum'] == 1 ? echo 'session_marks.php' : echo htmlentities($_SERVER['PHP_SELF']); ?>"
As for the number disappearing, I don't see any form element with the name sessionNum. If there isn't such a form element, then there will be nothing in $_POST['sessionNum'], so the number will "disappear" because there's no value to be displayed.
If the above is your actual code, session_start(); has to be placed before ANY other output (html, php's echo, print etc...)

PHP - Form: save entries/selections on form submit

I have a PHP form that has some drop down selections and text field entries. If the user selects the wrong item from the dropdown, when they submit the form, I have it so that it will show an error message to the user and force the browser to go back to the previous page. The problem is that the user has to re-enter all of the information.
How do I make the form save the data until the form submit is successful?
EDIT:
Form submit method is $_POST and the form is being submitted to another page.
This would have to be done with strictly PHP as Javascript/Jquery solutions can be script blocked by more secure users.
Here you go. This will work, and is not dependent on Javascript:
form.php //the form page
<?php session_start(); ?>
<form method="post" action="action.php">
<input type="text" id="input1" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input1'] : '') ?>" />
<input type="text" id="input2" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input2'] : '') ?>" />
</form>
action.php //the action page
<?php
session_start();
//do your validation here. If validation fails:
$_SESSION['fields']['input1'] = $_POST['input1'];
$_SESSION['fields']['input2'] = $_POST['input2'];
//redirect back to form.php
?>
Is the form a POST or a GET? Either way, you have access to all the submitted fields in the PHP variables $_POST or $_GET. Within your HTML you can pass those values (if set), to the default value of each HTML input element. This way, if it is a first time, they will be blank, if there was an error, the values will repopulate.
If they're select values, you can do something like this:
<select name="my_select" id="my_select">
<option value="123"<?php if($_REQUEST['my_select'] == 123) echo ' selected="selected"; ?>>123</option>
</select>
If you have regular text inputs, you can simply apply the $_REQUEST variable to the value attribute:
<input type="text" name="my_text" value="<?php echo $_REQUEST['my_text'] ?>" />
I suggest a preventing the page from navigating away from the submission until the data is verified. Enter jQuery :)
<script type="text/javascript" src="jquery-library.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Wait for the user to click on your button
$('#submit_button').click(function(){
// Check each form field for an appropriate value
if ($('#form_field1').val() != 'something I expect')
{
alert('Wrong submission!');
return false;
}
// Forward the user to some url location
window.location = 'url';
return false;
});
});
</script>

Categories