With a simple form, i have a field for users to input a date - when the fields are posted into my txt file i need them to be sorted by the date field, cant figure out a way to sort them by the date field, any ideas?
PHP Code:
if(isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
$data = 'REQUEST - ' . $_POST['field1'] . ' - ' . $_POST['field2'] . ' - ' . $_POST['field3'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);}
You're writing to the file in append mode, which only adds to the data already in the file. You're going to have to read the file back in,
add the data in sorted order, then re-write the entire file.
Using a database would probably be a simpler way to handle this.
Related
The question is simple but i will give some background information to hopefully make answering it easier.
So, I am working with the ELGG framework and taking information from a form and the text boxes in it in hopes to print that data to a simple text file.
Unfortunately, for some reason this has been giving me lots of trouble and I cannot seem to figure out why no matter where I look.
Code is as followed
<?php
error_reporting(E_ALL);
//Get the page
$title = get_input('title');
$body = get_input('body');
$date = date("l jS F Y h:i A");
//remove any possible bad text characters
$FileName = $title . ' ' . $date;
//print to file
//get filename and location to save
$folderName = '/Reports/' . $FileName . '.txt';
$ReportContent = "| " . $body . " |";
//write to the file
file_put_contents($folderName, $ReportContent, 0);
//error check to see if the file now exists
if (file_put_contents($folderName, $ReportContent)) {
system_message("Report Created (" . basename($folderName) . ")");
forward(REFERER);
} else {
register_error("Report (" . basename($folderName) . ") was not saved!");
forward(REFERER);
}
So what above SHOULD do is grab the text from the title and body box (which i can confirm it does from the title at least) and then save it under the /reports/ folder (full path for the plugin is Automated_script_view/reports/ if needed). Yet I will always get the register error, and I cannot seem to find why.
I believe it has to do with the declaration of the folder (/reports/), as if I take that part away, it passes and submits, although it doesn't seem to actually save anywhere.
Any and all advice would be very much appreciated!
The Function file_put_contents(file,data,mode,context) returns the number of character written into the file on success, or FALSE on failure. Note the following corrections and your script will work just fine:
1 Your File name has an 'illegal' character ":" coming from the $date part of the string you concatenate to form the filename.
2 Remove file_put_contents($folderName, $ReportContent, 0); Since the function returns an integer, simple use:
if( file_put_contents($folderName, $ReportContent) > 0 ){
//true
}else{
//false
}
I have a google map on this page, all markers were generated by submit postcodes. So I have the array below, loop info of each marker,
imploded as ("array", "array") format, I am trying to click on a infoWindow and display the according marker details on details.php.
The problem is everything is on the button onclick event, only a simple get on the second page.
This is working, but it is a very bad way. Because the limit to URL length and security reasons;
I would like to be able to get an array info from details.php page,
and the button onclick event trigger url looks like: details.php?marker=id
I don't know what is the best way to go about this, can someone pointing me to the right direction please?
index.php
$info = array();
foreach($stmt as $x)
{
$info[] =
"<h4>" . $x['name'] . "</h4><hr />".
"<h5>Address: </h5>" . $x['Address']."<br />" .
"<h5>Postcode: " . $x['postcode'] ."</h5><br />" .
"<button onclick='window.location.href= \\\"details.php?marker=". "<h4>" . $x['name'] . "</h4><hr />".
"<h5>Address: </h5>" . $x['address']."<br />" . "<h5>Postcode: " . $x['postcode'] ."</h5><br />" . "\\\" ' >
View Details</button>";
}
$i=' "'.implode('","', $info).'"';
details.php
echo $infomarker = $_GET['marker'];
You have to use $x['id'] insted of $x['name'] which is unique in your database and also use base64_encode() for encryption of your id "details.php?marker=".base64_encode($x['id'])."
In your details.php
$infomarker = base64_decode($_GET['marker']);
Try using AJAX to get the info from details.php and then load it into your InfoWindow.
I didn't realise how simple this was, all I need is use that id, write it inside a sql statement in details page then call any part of the statement out. Thanks everyone. Thanks to #Manjeet Barnala for encode tips.
My task is to put data from a form with 24 input fields into a txt file. Not very adapt at PHP coding I got a few snippets of code from the web, the file_put_contentsnippet from an example with 2 input fields. It worked (=it wrote data to file and showed the 'thank_you'-page). I added another field - it worked. Ok, so now I added all my fields - it did not write to file and produced a blank white page.
I went back to the 3 fields version, and started adding fields 1 by 1. When it stopped working at 8 fields, I introduced a 2. isset(POST[]) element. That way I could add another 6 fields, and it worked. (In between I separately saved the code when it successfully printed data from 11 fields to my file). When on the 7th field it again produced a blank page and did not save to file, I removed tha last addition - and to my BIG SURPRISE the very code that had worked before I added the last one didn't work any more?! I continued to remove element by element - it didn't work anymore?! So I reverted to the saved version of the code - and it still does not work.
So I went over every single letter, sign and number - all to no avail. Even though I havent changed anything in the html form, I ched this too, because everything points to a problem in the form that points to my php file. All in vain.
I am aware that it has to be something I did and now overlook - please point out to me where my mistake lies.
This is the version that worked 2 days ago, and I saved it before expanding:
<?php
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_POST['q01' | 'q02' | 'q03' | 'q04' | 'q05' | 'q06' | 'q07']) && isset($_POST['q08' | 'q09' | 'q99' | 'q98'])) {
$data = $_POST['q01'] . '#' . $_POST['q02'] . '#' . $_POST['q03'] . '#' . $_POST['q04'] . '#' . $_POST['q05'] . '#' . $_POST['q06'] . '#' . $_POST['q07'] . '#' . $_POST['q08'] . '#' . $_POST['q09'] . '#' . $_POST['q99'] . '#' . $_POST['q98'] . "\n";
$ret = file_put_contents('data/data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
header('Location:danke.html');
}
exit (0);
}
?>
Just to make sure I will also post the relevant lines of the form:
<form id="umfrage" class="appnitro" method="post" action="data2text.php" accept-charset="UTF-8">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Fertig!" />
your resetForm function does not seem to be PHP.
isset($_POST['q01' | 'q02' | 'q03' | 'q04' | 'q05' | 'q06' | 'q07'])
what are you expecting this to do? this does binary operations on string... This is not how you check array key availability. you should check each key individually:
if (isset($_POST['q01']) || isset($_POST['q02']) .....) {
note the double pipes!
Im trying to sent a message which contains the post values of a form. I dont think im putting them together right as its only sending the first POST value (make).
$message = $_POST['make'];
$_POST['model'];
$_POST['Street'];
$_POST['towncity'];
is this the correct way to put them together so that all 4 of them send?
Regards
Ross
There are many ways you can format this message. I encourage you to read this page of the PHP Documentation.
You can for example, concatenate all the values together (one on each line).
$message = $_POST['make'] . "\n"
. $_POST['model'] . "\n"
. $_POST['Street'] . "\n"
. $_POST['towncity'];
No, I don't think that would work!
Try:
$message= $_POST['make'] . $_POST['model'] . $_POST['Street'] . $_POST['towncity'];
But this would cause it to write everything in one line. I would suggest this:
$message="Make: " . $_POST['make'] . "\n Model: " . $_POST['model']; // etc...
Or if it's a HTML coded email, change \n to
$message = $_POST['make'] . $_POST['model'] . $_POST['Street'] . $_POST['towncity'];
BUT!
don't send values in the same form as they are posted. It's dangerous, and you might get exploited. Either make sure the content is properly cleaned, or even better, use library like Swift Mailer or PHPMailer.
Try this:
$message = $_POST['make'] . $_POST['model'] . $_POST['Street'] . $_POST['towncity'];
But be careful! Check for some dangerous php and mysql codes in the first place to make your code unhurt.
Use the concatenate operator. A semi-colon signifies the end of that current block of code. The concatenate strings the variables together:
$message = $_POST['make']
.$_POST['model']
.$_POST['Street']
.$_POST['towncity'];
Using $_POST values is very insecure however. Take a look at this example for a basic tutorial on how to protect against attacks and injections.
UPDATED: Examples added at end as requested:
Got a seemingly unique problem with concatenating form variables + the name of the button used to submit.
This works:
$word="number"; $number=1; myalbum=$word.$number;
echoing $myalbum gives "number1". No surprises there.
BUT if I POST the form to a php script with the intention of writing to file ONLY the data on row on which the button was pushed, I get problems.
So, let's say I've got 10 rows, and the button for row 5 is pushed.
If I get the script to echo what button was pushed ($button), I get "5" back.
If I get the script to echo what's in the box in row 5 (in this case, "$number5=5") then by echoing $number5 I get 5.
But if I concatenate $number.$button, I get nothing, when I expect "number5".
And yet, if I concat any two parts of the submitted data, it works as expected.
I've been over the variables section at php.net, I've been over the w3 forms tutorials.
I've googled. I've stackoverflowed. I've checked and triple checked my spelling.
I even started from scratch - again, it's almost as if appending the value of the button kills the concatenation process.
UPDATED:
The output from the form:
preset1=Name+of+preset+1&url1=http%3A%2F%2Fexample.com%2F1&preset2=Name+of+preset+2&url2=http%3A%2F%2Fexample.com%2F2&preset3=Name+of+preset+3&url3=http%3A%2F%2Fexample.com%2F3
The code for the form handler:
<?php
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Preset: " . $preset . " - Title:" . $title . $submitButton . " - Submit Button:" . $submitButton . "\n";
fwrite($fh, $stringData);
fclose($fh);
?>
The output from the above:
Preset: 3 - Title:3 - Submit Button:3
So, we know it knows what buttons has been pressed. But not the output I expected.
But if I change the line to
$stringData = "Preset: " . $preset3 . " - Title:" . $title3 . " - Submit Button:" . $submitButton . "\n";
then I get, as expected:
Preset: Name of preset 3 - Title:http://www.example.com/3 - Submit Button:3
But of course, this is no good. I understood that if
$preset.$submitButton would be the same as $preset3 if submitButton was 3.
Oh, and I've also tried
$thepreset='$title' . $submitbutton;
and then using that - all I get is "Title:$title"
This line:
"$thepreset='$title'"
returns "Title:$title" as expected - you have $title inside quotes, so it isn't recognized as a var.