I am working on a form, and want to save a copy of every form submitted. The problem is the form's action is a counting file, that makes each file saved count up once. For example, the third form submitted is named "3.php", and the tenth form submitted is named "10.php". When I try to write the POST variable top the new file, it is gone. Anyway I could write their responses to a new document with my counting files code?
Form code on main file:
<form action="count.php" method="post">
<input type="text" name="formItem1">
<input type="text" name="formItem2" required>
<input type="text" name="formItem4" required>
<input type="text" name="formItem5" required>
<input type="text" name="formItem6" required>
<input type="submit" name="Click" value="Submit">
</form>
Count.php code:
<body>
<?php
define('countlog.txt', __DIR__ . "./");
if (file_exists('countlog.txt')) {
# If File exists - read its content
$start = (int) file_get_contents('countlog.txt');
} else {
# If No File Found - Create new File
$start = 1;
#file_put_contents('countlog.txt', "$start");
}
# When Form Submitted
if (isset($_POST) and isset($_POST['Click']) and $_POST['Click'] == "Submit") {
$file_name = "{$start}.php";
$template = file_get_contents('template.php');
#file_put_contents("./submissions/" . $file_name, "$template");
# Update Counter too
$start = $start + 1;
#file_put_contents('countlog.txt', "$start", 1);
echo "Generated Filename - $file_name";
}
?>
</body>
Template.php code:
echo "<h1>Answer1: " . $formItem1 . "</h1>";
echo "<h1>Answer2: " . $formItem2 . "</h1>";
echo "<h1>Answer3: " . $formItem3 . "</h1>";
echo "<h1>Answer4: " . $formItem4 . "</h1>";
echo "<h1>Answer5: " . $formItem5 . "</h1>";
echo "<h1>Answer: " . $formItem6 . "</h1>";
Use sessions. Create a session on each page with session_start(); Store the post value using the session global array. eg. $_SESSION['yourValue'] = POST['yourValue'];. Now on the rest of the pages you should be able to access the value. e.g
$yourValue = $_SESSION['yourValue'];
IF you use $template = file_get_contents('template.php'); it will make the content of template.php a string, and the variables will not be evaluated.
You could use eval() http://php.net/manual/en/function.eval.php to evaluate a string as PHP code. I would not recommend this method, but if you want to use it you will need to return, rather than echo the template otherwise the template will be echoed to the browser rather than the string you want to save to file.
template.php
return "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";
count.php
$template = file_get_contents('template.php');
$template = eval($template);
It would be much easier/better to include the template, and the code will execute, thus populating the $template variable.
template.php
<?php
$template = "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";
?>
count.php
include('template.php');
Both methods assume you have used extract($_POST); to import variables from an array into the current symbol table.
Related
I'm setting a variable based on an array. When I echo the variable it displays on the screen, however when I try to add it to a Header Location it doesn't show up in the URL of the next page - everything else does:
$myid = $selected_cat3[0]['id'];
Header("Location:/cat-dashboard/cat-results/?catID=" . $myid
. "&question1=" . $_GET['question1'] . "&question2=" . $_GET['question2']
. "&question3=" . $_GET['question3'] . "&question4=" . $_GET['question4']
. "&question5=" . $_GET['question5'] . "&question6=" . $_GET['question6']
. "&question7=" . $_GET['question7'] . "&question8=" . $_GET['question8']);
This is the generated url:
/cat-dashboard/cat-results/?catID=&question1=0&question2=3&question3=1&question4=1&question5=1&question6=2&question7=1&question8=3
Am I doing something wrong? It doesn't show even if I use: $myid = "1";
I'm having trouble sending a parameter into a url of another page. I'm new to coding in PHP so I do not really know how to get this through, already did some research on this about the $_GET method, but its still not working.
Code in 1st page:
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</a></td></tr>";
where $temp is the parameter I want to pass to the url.
Code in 2nd page:
$id = $_GET['id'];
$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = $id");
//$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = 10");
The sql query returns error that the $id is null, and the url doesn't display the id.
Do it like this on your html line
echo "<tr><td>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</td></tr>";
I ran your code to see what it output:
// Make some sample data
$row = [
'appl_nric_date' => '9999-99-99',
'applicant_name' => 'some-applicant',
'nric' => 'wtf-is-an-nric',
'application_date' => '8888-99-00'
];
$temp = 'something';
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>"
. $row['appl_nric_date']
. "</td><td>"
. $row['applicant_name']
. "</td><td>"
. $row['nric']
. "</td><td>"
. $row['application_date'] . "</a></td></tr>";
echo PHP_EOL;
This is what it outputs:
<tr><td><a href='application_desktop.php?id='something'>9999-99-99</td><td>some-applicant</td><td>wtf-is-an-nric</td><td>8888-99-00</a></td></tr>
The use of single quotes is not right. Remove the single quote after id=.
Looks like you are not nesting your html elements correctly. You place the opening A tag inside the first TD but then you close that TD without closing the A tag.
In order to debug what you are doing this in the browser, then the address bar where the url lives should show the parameters that are sent to the destination page. You can just look at that to verify that it sent what you intended.
In the destination page, you can add the following to debug:
<pre>
<?php print_r($_GET) ?>
</pre>
The above will let you see what you are getting from the first page.
I'm trying to write a basic example of php that I want to be viewed on my website. In other words I want the following code to be viewable but I would also like a copy of it to be executable on the webpage:
<body>
<p>
<?php
$food = array("Bananas", "Toast", "Eggs", "Bacon");
echo "I like " . $food[0] . ", " . $food[1] .
" and " . $food[2] . " and " . $food[3] . ".";
?>
</p>
</body>
<?php and ?> makes code inside to execute, use <?php and ?>
I have just a quick question. i was using a normal html link tag to redirect to a paypal checkout page and it was working fine even when i had php inside the url. but when i was using it in a php header
the url cuts off where i enter the php.
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product . " " . $server ?>&amount=<? echo $xprice1; ?>%2e00¤cy_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
You are placing the PHP code inside of the location redirect as a string. The code is not being evaluated as PHP.
Try this instead:
<?php
$url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00¤cy_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest";
header('location: ' . $url);
Or, you could keep it in one line like so:
<?php
header('location: ' . "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00¤cy_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest");
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product' . " " . '$server ?>&amount=<? echo $xprice1; ?>%2e00¤cy_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
This may solve your problem. Please dont forget to tick the answer right if it works.
I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an XML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}
I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.
Check out htmlspecialchars and htmlentities
use CDATA construction:
echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";
If this is your entire script, fastest way would probably be to swap all of the <'s with <
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}