I am trying to write a form's content to a text file on my computer, which is from where the server is running, and I can successfully write variables that are defined like$newVar = "Text to write."; , but when I try to pass a value from a form in my HTML code by $newVar = $_POST["varOutput"]; , nothing will be written in the file. My HTML form appears as so:
<form id="hidden" action="submit.php" method="post">
<textarea name="varOutput" form="hidden" rows="73" cols="100" id="varholder"></textarea>
</form>
I have some JavaScript that puts a string into the text area, but the string is different for each user, as they will interact with the website differently. I have tried countless times to change every little detail in my code to make it work correctly, but my efforts have been fruitless. Any help would be appreciated.
EDIT:
The PHP code is
$output = $_POST["varOutput"];
$fp = fopen("text1.txt","a");
$savestring = ($output . ānā);
fwrite($fp,$savestring);
fclose($fp);
That is the entirety of the php file that the javascript references. The "n" is to test to see if anything happens. It prints every time.
Debug
1) check if the value is set in textarea.
You can use firebug or inspect hidden elements to know if value is set correctly.
2) print all posted data to on php to know if your data is posted to backend.
After hours of trying different ways, I found out that I could get the $_POST to work if I used an <input type="submit" /> element inside the form. This allowed the php variable to read the text inside the form and write it to my text file. Thanks for everyone's help, and I hope this will help someone else. Have a nice day.
Related
I have started learning php and I have a question.Let's say I have the following html code:
<p id='tobeChanged'>I wil be changed throughout the execution<p>
This paragraph is not static.Its content can be changed from the user with a button which will produce a random number and will replace the paragraphs html.
E.g. from
p id='tobeChanged'>I wil be changed throughout the execution<p>
to
<p id='tobeChanged'>42<p><!--changed with a button-->
Now my question.Is it possible to pass the new produced value to a php variable?If possible i would like a long explanation.
Also i would like not to use forms(if possible).
Thanks In advance
You need to fire an AJAX request on that button click, that will send that value to server making php to read it.
You can do something like this (you need to include jQuery on page):
$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})
At PHP end, you will get the value in
$_POST['randNum']
Maybe that will help.
I have a php with a form/text area, I do not want to use a button press to post data to some other PHP.
I am looking for some auto_post AJAX query which will put all the data that user has typed in the text box, into a $variable in the same php.
Something like this:
<?php
$checkbox.= '<input type="textbox" name="vehicle" value="" />';
echo "$checkbox<br>";
$return = $checkbox;
//$return should have all the data typed by the user
?>
This is not possible without PHP as it is a serverside language, which means, that all code is processed before the document is sent to the Browser. Ajax and JavaScript however are Clientside languages, which first come to work when the Client recieved the document.
Working with php asynchronus can be reached as you already guessed by using Ajax. By sending information to a php script and printig it as a xml document you can run what you want to progress and read it on the original page again using Javascript.
But i suggest to read a tutorial on Ajax itself to get an idea of what to do, as this is a bit too much for a single Stackoverflow answer.
Here are some tutorials that might help you:
http://killerajax.com/
https://developer.mozilla.org/
I swear i couldn't find a simple working solution for this.
On a form i have inputs that have names containing "[]" and i cant change the names of the inputs because they are part of a script.
I want to php POST the values of those inputs at the next page, after the form submit.
Example of input
<input type="text" name="CustomFields[13]" id="CustomFields_13_1" value="">
Anyone knows how to accomplish it?
I want to do it using PHP only
If the name is CustomFields[13], then you can access it at $_POST['CustomFields']['13'].
You "cannot" POST something with PHP. It's always the client that POSTs to the server. PHP is running on server side.
I recommend that you use sessions and save there the values that you need to have available in next pages.
This is how you set a session:
session_start();
$_SESSION['CustomField'] = "test";
And this is how you get it:
session_start();
echo $_SESSION['CustomField']; //Should display "test"
I've been trying to create some php code that would let me
take input from a user using a text box. I want to then store
that input (probably like 7 characters long) and then display it
on a page in a list format ( eg
1
2
3
)
I've been at this for hours reading tutorials and what not but everything
I find only shows me the fwrite function and when I try to append I still
end up deleting data.
I would like the newest inputs to show up on top of the list if possible.
Can someone help me?
I am really frustrated and I know almost no php.. kind of playing around
with it and can't figure this out =/
I was thinking of storing the input from users in an array, then display the array..
But that doesn't work for me either.
So you want to have a form, let the users enter text, then display all the text entered by all the users, sort of like a wall or guestbook.
I'm presuming you've managed to fetch the user's input by looking at $_POST after the form submission. To store it in a file without overwriting the existing file contents the easiest way is file_put_contents with the special FILE_APPEND flag.
Lets say your HTML form has a textbox with name="newData". Then, in the form submission target script:
//store all user input in a file called data.txt in the current directory
$filename = "./data.txt" ;
$newData = $_POST['newData'] . "\n" ;
file_put_contents($filename, $newData, FILE_APPEND);
//now fetch all data and display it
$lines = file($filename) ;
echo '<ul>' ;
foreach ($lines as $line) {
echo "<li>$line</li>" ;
}
echo '</ul>' ;
Get started like that to see the basics in action and then you can look into:
filtering user input so that you don't store and display any nasty stuff
storing the data file outside the web root so that it's not accessible via the browser
prettifying the output list
If they're submitting the data via form, it will POST to the server; meaning you can use:
<form action="target.php" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit value="Submit" />
</form>
for the markup, and then for the PHP (on target.php):
<?php
if (isset($_POST['submit']) { //to check if the form was submitted
$username= $_POST['username'];
}
?>
At this point you can then use <?php echo $username ?> anywhere you want to echo the data entered from the previous page.
I'm no PHP expert either (I recommend the Lynda.com training videos, "PHP & MySQL Essential Training" and "PHP & MySQL Beyond The Basics" with Kevin Skoglund) but maybe this helps.
As said above, you can use $_POST to get the data, however it is worth noting that if you are actually using this in a web application, you need to sanitize for XSS. You can do this quite simply with PHP's htmlspecialchars()
try this for taking input from user in php
$variablname = fgets(STDIN);
echo $variable;
file_put_contents('/path/to/file','contents',FILE_APPEND);
i have a page,say abc.html, that is having a small form with some fields.
<form name="form" method="post" action="abc.html">.......................</form>
when we submit the form it again comes back to abc.html with some data posted and shows the resulted names on the page which came after processing the posted data.
in the whole procedure the page url remains same.Now i want to parse this abc.html containing data after the submission of the form.I have done parsing in which the original url contains all the data but not like this on which after submission the data gets displayed on the page.Please tell me how can i parse such page??
Well, to get the correct HTML from the server, you have to send a POST request containing the form data. Then you can parse the server response.
Parsing the HTML file is same as us seeing it. So the HTML page rendered after posting the data will have some or any HTML element in which the additional text is displayed. When you parse the page chek if this or a container exists if so then read the rest of the data. The HTML page displayed without the posted data will not have this additional or container.
Edit: Look at this question : PHP Screen Scraping and Sessions
First of all. Your page should be abc.php. Otherwise it will not parse any php.
Second. Here is some code that will help you out (I Hope). Copy/Paste this example and place it in abc.php
<html>
<head></head>
<body>
<?php
if (isset($_POST['submit'])) {
echo 'you posted the following value :'.$_POST['foo'];
}
?>
<form name="form" action="abc.php" method="post">
<input type="text" name="foo" value="" />
<input type="submit" name="submit" value="Press Me" />
</form>
</body>
</html>
If this is not the case. And you want to parse HTML like parsing XML you should use the DOMDocument class of PHP
$oDom = new DOMDocument();
$oDom->loadHTML($sHTMLstring);
// or
$oDom->loadHTMLFile($sFileName);
// now you can walk the dom like
$oDomElement = $oDom->getElementByTagName('form');
http://nl.php.net/manual/en/domdocument.loadhtml.php
http://nl.php.net/manual/en/domdocument.loadhtmlfile.php
http://nl.php.net/manual/en/domdocument.getelementsbytagname.php
Hope this helps
Good question, but I think it's not possible with PHP. My company doing that with very advanced tool in C. It just grab any page and send the any form and get rsponse HTML.
But You can found maybe some tools. Don't know.
I think the point here is that you can't just open the URL and read the HTML that comes back. You will have to play the part of the browser in order to interact with the server side form. To do this, you'll have to write your own code to HTTP POST the form input data. The HTTP response to your POST will contain the generated HTML, which you can then parse for the processed results.
If you want to send the form to the web server (i.e. "fill" it first) you need something similar to Perls WWW::Mechanize. See this question for possible solutions to do this. Afterwards, you need to parse the resulting page, and that heavily depends on the site in question itself: one site might use named elements you can easily retrieve using regular expressions, a different site might not, making it much harder to get the values you're interested in.