Get inner html out of an HTML template used with PHP - php

So I have a PHP code similar to this
<?php
//some code here
?>
<form>
<input />
</form>
And according to this example, I want to get the input's content to use further in PHP and/or insert in the desired input some variables from my PHP code, how can I perform this?

You can post the data to php from a HTML form
<?php
// data is available in php POST array
var_dump($_POST);
// create a var to hold the post data
$sNameOfPostVar = "";
// if posted, set the var to the value of the posted content
if(isset($_POST['NameOfPostVar'])){
$sNameOfPostVar = $_POST['NameOfPostVar'];
}
?>
<!-- current action is blank to send to same page as php script -->
<form method='post' action=''>
<input name='NameOfPostVar' value='<?php echo $sNameOfPostVar;?>' />
</form>

Related

Add data to $_POST and submit to another page

I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.

trouble with extracting form data from another php page

I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}

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 can I send contents of a div as a POST parameter?

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!

Displaying Database Query Results

Suppose I have a form. After I submit my form, the data is submitted to dataprocess.php file.
The dataprocess.php file processes the variable sent via form and echoes desirable output.
It seems impossible to echo to a specified div in specified page only using PHP (without using AJAX/JavaScript as well). I do not want to use these because some browsers might have these disabled.
My concern is that I want to maintain the same formatting of the page that contained the form element. I want the form element to be there as well. I want the query result to be displayed below the form.
I could echo exact html code with some modification but that's memory expensive and I want it systematic.
Is it possible to process the form within the same page? Instead of asking another .php file to process it? How does one implement it?
The above is just for knowledge. It will be long and messy to include the PHP script within the same HTML file. Also, that method might not be efficient if I have same process.php file being used by several forms.
I am actually looking for efficient methods. How do web developers display query result in same page? Do the echo all the html formatting? also, does disabling JavaScript disable jQuery/AJAX?
Yes it is possible to process the form on the same page.
<?php
if (isset($POST))
{
//write your insert query
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- Your form elements and submit button -->
</form>
<table>
<?php
//your select query in a while loop
?>
</table>
</body>
</html>
But if you choose this technique instead of ajax, you have to refresh all the page for each insert action.
An example
<div id="dialog-form">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>Job</td>
<td>
<input type="text" name="job" />
</td>
</tr
</table>
<input type="submit" value="Insert" />
</fieldset>
<input type="hidden" name="doProcess" value="Yes" />
</form>
</div>
<?php
$myQuery= $db->prepare("INSERT INTO Jobs (job) VALUES (:p1)");
if (isset($_POST['doProcess']) && $_POST['doProcess'] == 'Yes')
{
$myQuery->bindValue(":p1", $_POST['job'], PDO::PARAM_STR);
$myQuery->execute();
}
?>
if you really dont want to use ajax (which i think you should). You can do something like this.
<form action="" method="POST">
<input type="text" value="something" name="something_name"/>
<?php
if(isset($_POST['something_name'])){
echo '<div id="display_something_name_if_exists">';
echo $_POST['something_name'];
echo '</div>';
}
?>
</form>
Basically what it does is submits to itself and then if there is a submission (tested with isset), it will echo a div with the correct information.

Categories