I am building a PHP website that has one page that contains all of the UI (header, footer, etc.), and other pages that just contain the content. In my main file, between the header and footer, I put the code:
<?php
echo $_POST["content"];
?>
I was planning to also provide the user with a hyperlink that would refresh the page while sending POST data, in this case the contents of one of the other php content files. How would I create this link?
Thank you very much,
Cello Guy
You should use JavaScript to send the form, here is an example with jQuery:
<form id="myForm">
<input type="hidden" name="content" value="<?= htmlspecialchars($content) ?>">
</form>
Send
<script>
$(function(){
$('#myLink').click(function(){
$('#myForm').submit();
});
});
</script>
Related
I have a form in html that post income to incomesave.php where session is initiated and save all the form contents to session.
The problem is that I want to stay in the same page when submitting form all the while saving data to session and use that session values to populate the income history on the same page. How can I stay in the same page and process the data with php?
This is the form inside html:
<form action="lib/php/incomesave.php" method="post">
$: <input type="number" name="price">
type: <input type="text" name="source" >
<input type="submit" value="Submit">
</form>
<div class="row">
<div class="twelve columns">
<div class="title">
Income history
</div>
<div id="incomehistory">
</div>
</div>
</div>
and this is the incomesave.php to save the form data
<?php
session_start();
$price = $_POST['price'];
$source = $_POST['source'];
$_SESSION['price'] = $price;
$_SESSION['source'] = $source;
echo $_SESSION['price']; // print price
echo $_SESSION['source'];
?>
I want to stay on the same page, run the incomesave.php and populate the income history area of the html page with $_SESSION data. How can I do that? any help would be appreciated!
Submit a AJAX requet to the server and modify the DOM with the result.
xhttp.open("GET", "incomesave.php", false);
xhttp.send();
document.getElementById("incomehistory").innerHTML = xhttp.responseText;
Use ajax to post the data form without changing the page and after the ajax call, save the data inside incomesave.php and after that, use callback or success method to change the html using .innerHTML
thank you to all the comments to lead up to the answer!
If you don't want the page to refresh you have to use ajax to complete this. I'm not gonna post a code example since I don't know how familiar you are with ajax post.
Do a research on ajax posting and result processing. If you are familiar with ajax post, let me know so I can do some code example..
IF you just want to stay on same page, check the answer I posted earlier on sessions
Simple session example not working
if you set form action it will redirect you to action url page. On that page you initialize all session data and then redirect it to same page using header like
header('URL');
url will be html page url.
Take this simple form as an example:
<form method="post" action="reports.php">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Submit">
I want it to send the input fields data to the reports.php page but I want it to direct the user to the page uploads.php when he clicks the "Submit" button.
Using header("Location: uploads.php") on reports.php page is not an option because reports.php gathers the form data into a table for everyone to see. In the uploads.php page, the user will upload files that will also be visible on reports.php table.
How do I do this?
You can check if the method used in request to reports.php is POST, and redirect the user.
<?php
# reports.php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
// process the data
header("Location: ...");
exit;
}
Or you can simply create a add_report.php file.
You could add "on form submit" event with javascript.
When the submit button is clicked - collect the form data with javascript and send it where is necessary. When the ajax worked - redirect the user to the desired page with javascript.
A complete example would be smth like:
for html
<form method="post" id="my_form">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Submit">
for javascript
<script>
$(function(){
$('#my_form').on('submit', function(e){
e.preventDefault();
var url = 'reports.php?' + $(this).serialize();
$.get(url, function(){
window.location.href = 'uploads.php';
});
})
})
</script>
more info can be found here:
https://api.jquery.com/serialize/
You need to refactor your code.
It sounds like your use cases are:
GET to uploads: Show uploads form
POST to somewhere: Add to reports and then show uploads form
GET to reports: Show reports list
POST to reports: Add to reports and then show uploads form
Don't worry about making the "somewhere" be the reports file.
Separate out the "Add to reports" code into a function (or functions) and put them into a file of their own.
Then include that file in both reports and uploads.
Make the upload form submit to uploads and then call the "Add to reports" function.
Do something similar in reports.
I have built a site using php and want to try keep it one page.. The site displays pictures and so far i have it making links from folders in a folder each folder contains images so what i want is to make a post/get tag in the url and when the page loads it uses this to get the images from that folder.
So I want to use the generated links to post to the same page with a value via self_post is this possible and if so how?
my get section is
if(empty($_post['foldername']))
{
$directory = "portfolio/homepage/";
}
else if(isset($_post['foldername']))
{
$foldername = $_post['foldername'];
$directory = "portfolio/".$foldername."/";
}
and my link is like this
echo '<li><a id="" class="" href="'.$_SERVER["PHP_SELF"].'">'.$b.'<input type="hidden" name="foldername" value="'.$b.'" /></a></li>';
Thanks
What's wrong with GET?
Click me
The only way to make a POST request using a <a> tag would be to have it submit a form via javascript:
<form method="post" id="hidden_form" name="hidden_form" action="script.php">
<input type="hidden" name="foldername" value="<?php echo $b ?>" />
</form>
...
post me
You can also update the values of the hidden element(s) from javascript as well so when you click a particular link, it sets one of the values to something specific to the link.
The only way is doing it through JS. You can either send an AJAX request specifying POST, or you can create a hidden form and submit it. Here's an example
document.getElementById('my-link').onclick = function(){
// Code to submit the hidden form or to send an AJAX request specifying POST
return false; // to prevent the default behavior
}
I know of no way to do this with vanilla anchor tags. You could establish click event handlers in javascript to submit an XHR request. However, I have accomplished this in the past by using multiple form tags with a single submit entity (<input type='submit', <button type='submit', etc.).
Your forms would look like so:
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="hidden" name="foldername" value="YOURVALUEHERE">
<input type="submit">
</form>
Like drew010 said if you absolutly need the POST method. Otherwise most single-page website uses things like index.php?a=1&b=2 where you can get "a" and "b" with $_GET["a"] ...
I'm trying to implement popup search window in a PHP project. I included JQuery and ColorBox & have managed to open the Search popup inline (using ColorBox plugin).
This is my code to open popup window
$(document).ready(function(){
$(".inline").colorbox({inline:true, transition:'none',speed:'10', close:'close', opacity:'0.6'});
});
Popup div has a separate <form> element to POST data.
<?php
if (isset($_POST['btnSearch']))
{
//Code to search data
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
//Some Page Content
<div class="input"><input type="submit" name="btnSearch" Value="Search"
class="button"></div>
</form>
The problem is when I click the 'Search' button it POST the form but closes the popup. How can I retain popup window opened even after the button click?
AJAX is what you're looking for. jQuery has built in AJAX support which you can read about HERE
It's real easy to use so you shouldn't have any problems implementing it
I have a problem. I need to transfer a message that a user types to the same page (cause I need to save it to the database). I have a form and a 'submit button'.
If I use the command 'post' I am able to save transfer the data to the same page and save it after extracting the post variables.
Problem is that I dont send the page_id in the url (I could acheive it with GET method, but I cant use it, cause the users message could be too long to go into the url).
I tried to do some javascript code, to transfer the 'submit' button to simple button and do a redirect..:
<script type="text/javascript">
document.getElementById('messageSent').addEventListener('click', function () {
window.location = 'article.php?articleID=<?php echo $_REQUEST['pageID']; ?>';
}, false);
</script>
But then the POST variables are lost.
Basically, what I want is to achieve the effect of post and get, I want to post the the variables while encoding the url to look like this:
home.php?pageID=<?php echo $_REQUEST['pageID']; ?>
The page should repost to itself as post request with the pageID set..
How do I achieve that?!?
You'll have to dynamically build a form, populate it with the POST values, and submit it using JavaScript:
<form id="myform" action="target.php" method="post">
<input type="hidden" value="<?php echo $_POST['pageID']; ?>">
</form>
<script>
// ... eventually....
document.getElementById("myform").submit();
</script>
You should use an URL containing the page id in action of form. For example:
<form action="home.php?pageID=<?php echo $_REQUEST['pageID']; ?>" method="post">
</form>