Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax? Do I have to return html data like this: Your post is <?php echo htmlspecialchars($_POST["email"]); ?>.<br> If so how do I do that and still keep all the other elements on the page the same way they are currently formatted. Related to this, how do I load the HTML page with some elements already bound to values extracted from the database using an external PHP file?
What is best practice for doing this kind of thing. The objective is to allow a user to input text in a textbox, click submit to have the text stored in a database, and then at the same time update an element that displays the latest sumbmission, kind of like a forum post. I know you can use ajax, javascript, jquery to do this kind of thing on the client side, but I have seen html pages with the <?php echo htmlspecialchars($_POST["email"]); ?> type syntax and wanted to experiment with that for simplicity.
"Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax?"
Answer: No, you must send the data to the same page, ( action="" )
Then include the processing file at the top/beginning of the script. Please feel free to ask for more help.
IF YOU WANT TO SEND THE DATA TO THE DATABASE AND WANT TO DISPLAY THE DATA: Just retrieve the data from your database on the page you want to display it on.
Related
I'm really not good at technical terms (because I'm still a beginner) but I am developing a system as a school project. It's a mobile app (I used phonegap). my question is that I wanted to send data (preferably $_get) to a PHP page so that I can run my query with it. But I don't want a button or link to be a trigger. I want my trigger to be when the page reloads (or refreshes. Any idea or help is appreciated!
You can do that with javascript, binding the action of sending data to your php page to the "document loaded" event.
With pure javascript you can do that with
<body onload="yourFunction();">
or
document.onload = function ...
If you are working with PHP, you should create the HTML dynamically so, the information sent to the app will already contain the data when shown.
A dirty example:
<?php
?>
<html>
static html code
<?php
your content including dynamic data shown from your PHP backend
?>
</html>
I'm using web2pdf to print a few values to a pdf document. In the html, these values show but when I'm converting the webpage to pdf, these php/js values disappear. What can I do to fix this?
<html>
<body>
<?php session_start();
$variable = $_SESSION['getSessionVar'];
?>
<span id = "name"><?php echo $variable ?></span>
<button id="cmd" > generate PDF </button>
..tags closed.
When I run this script, the variable value is shown but the PDF converter is not picking it up. Is there anything wrong with my code? Else, what other web-to-pdf generator can I use that can pick up Php or JS values written to HTML?
Thanks!
Update -
I can't seem to convert any GET/POST or SESSION variables in the php script to PDF even though I assign them to a html division. Also tried assigning php variable to js and using
document.getElementById('ID').innerHTML = "<?php echo #var_from_prev_page"?>";
However, I can print and convert php variables that I create on the page.
SESSION,POST and GET can't be show in your pdf.
the reason is pretty simple actually.
The site you are using is using the reference of the request to "Crawl" your page
So the session id they get is diferent than you therefore they can't have any $_SESSION loaded since they never browsed your site before, and they don't POST anything to the page since you are clicking on a link and not sending them any data to send back to your site.
The only way you could acheive what you want to do would be to have some way to change your ref of your request and add some GET by clicking the a link and have something to load variables in your php script.
If you want to test this actually, What you could do is to compare echo session_id(); output from your browser and the pdf you get.
This is an odd situation and my current thought is that it doesn't work this way, but I need to some other eyes on this.
A different website I don't have control over has a form with a hidden field in it. The form action is a POST and to send it to a url on my website and I need to be able to get the value of that hidden field using javascript.
As a GET that would be included in the url and I think I would just be parsing that apart. But since it's a POST being sent to me I'm not entirely sure how to get the value of that hidden field out.
Is this doable? If so, where should I be looking to do it?
Thank you!
If your server that is receiving the sended form data uses PHP, you can get all form values using:
<?php
print_r($_POST);
?>
If the page in your server is a static html page, then you cannot get the POST data. Or you can, but then you have to make html pages to be executed as php pages (not recommended however).
You talk about that you need this value be accessible by javascript. Simply do something like:
<script>
<?php
echo 'var input_field_value="'.htmlspecialchars($_POST['name_of_input_field']).'";';
?>
</script>
The question doesn't provide information what server software is used, so I assume that is PHP.
EDIT: after Saturnix's comment I added a call to htmlspecialchars() to make it safe to execute in javascript.
Main problem is values written in input elementss disseapear after page reload (submit , refresh etc.)
I have a completed form ... /form element. Traditionally, I can insert a php line.
<input value="<?php if(isset($_POST['foo']))echo $_POST['foo'] ?>">
This solves the submit part. However, I feel that this is the worst solution, in my case. I have hundreds of input elements in my form. There are even some inputs to produce input tables. Number of input elements are so much that i became curious about finding a work around.
Is there a way to store input->values before the submit operation and inject them after page reload?
So that, the user can upload a file, file will be parsed by php core. And when the page reloaded both user originated inputs and file originated values are exist.
To simplify:
After "file submit & read & append file values to form", user shouldn't need to fill inputs that s/he already filled. I need an idea to achieve this, different then "inserting a php line to every single input element."
In such a situation I could recommend sending the file via AJAX and handling the response of that thereafter and then only injecting the values from the process and uploaded file when you get the response from the server.
Alternatively you could use localstorage or cookies to persist the information. However both local storage and cookies have a defined limit on what they can store. Cookie can only store 4KB in total which doesn't allow much.
Without uploading via AJAX, you could write a javascript function to find all inputs with jQuery/javascript and save their values in localstorage and on new page load to a check to see if there are any present and inject them back into the same inputs based on id/class/ etc with jQuery making sure to delete the localstorage values when done.
Client goes to example.com/form.html where a html POST form is displayed
Client fills the form with specific information and submit it to example.com/form.html
When example.com/form.html receives the POST request redirects the Client on example.com/redirected.html
Is possible to retrieve the variables that the client filled and POSTed to example.com/form using javascript ? The javaScript being deployed on example.com/redirected.html only . I presume that can be some "back" controls iframes and ajax involved but I couldn't find a reliable solution yet.
The operation will take place on the same domain so no cross domain issue is involved
Nope, I don't think this is possible.
You have to either
Store the posted value in a cookie
Store the posted value in a session variable on server side
Add the posted value as a GET parameter to the new URL
Use GET to post the original form, and painfully extract the posted value from document.referer on the new page
With HTML5, use localstorage. (The answer describes how to store object in localstorage- you could store an object of your form fields in there).
But you have to store the data on posting with js at example.com/form.html and then can get it on example.com/redirected.html. Without js at form.html, this is not possible with this method.
This works if you plan to use html5 and do not store too much data in it, iirc it has a limit of 5-10mb depending on the browser.
I don't think there is a way to do this by using plain html. With some server-side language (like PHP) it can be done with no problem.
I have been in a similar situation before, and the way I managed to give the data to JS is by including the data in a tag while preparing the output using PHP.
Assuming the redirected to php script receives the POST data from the script it's being redirected in. I would include this in the php code:
<?php
echo '<script type="text/javascript">';
echo 'var postData = '.json_encode($_POST).';';
echo '</script>'
?>
This will have the javascript know what the POST values contained.
To access the values from js:
//assuming you need the value for $_POST['foo']
var foo = postData.foo;
// or if json is encoded as just an associative array
var foo = postData['foo'];
If the POST data is not being passed to the redirected to script (haven't checked if this happens automatically), you could write the $_POST data in a $_SESSION variable, in the first php script:
<?php
$_SESSION['postdata']=$_POST;
?>
and then get it back from SESSION from the redirected to script.
<?php
$postdata = $_SESSION['postdata']; //use this to create the inline script in the html
unset($_SESSION['postdata']; //empty this from the SESSION variables
?>