I am following an online tutorial on php and i'm having a problem. I have no idea what could be the problem
<h1>Order Online!</h1>
<form action="order.php" method="POST">
Bread (0.99 USD):
<input type="text" name="bread" />
<br /><br />
Cookie (1.29 USD):
<input type="text" name="cookie" />
<br /><br />
<input type="submit" value="Submit order" />
</form>
This file is supposed to take two numbers (yes I know that the inputs should be type="number") and send them to order.php
<?php
$bread = $_POST['bread'];
$cookie = $_POST['cookie'];
$sum = 0.99 * $bread + 1.29 * $cookie;
echo<<<END
<h2>Order Summary</h2>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>Bread (0.99 USD)</td> <td>$bread</td>
</tr>
<tr>
<td>Cookie (1.29 USD)</td> <td>$Cookie</td>
</tr>
<tr>
<td>Total</td> <td>$sum USD</td>
</tr>
</table>
END;
?>
In the video everything was working perfectly, meanwhile everytime i try it, it results with
this:
Earlier today I was able to run a php file perfectly fine, and now i have been stuck with this problem.
Related
i'm trying to create a simple input program for an online site however when i try to insert data with
query : INSERT INTO tbl_integritytype (type) VALUE ('$type')
With the $type it self is $type=$_POST['ref22'];.
But the output of the $type is always blank
i used :
<form action="insert_integrity.php" method="POST">
<tr class="content-specific-pg">
<td width="104" height="26"><div align="left" class="style6">
<div align="left">Type </div>
</div></td>
<td width="10"><div align="right" class="style6"> :</div></td>
<td width="126"><span class="style6">
<input type="text" id="ref22" name="ref22" size="20" class="easyui-textbox"/>
</span></td>
<td><td width="141"><input type="submit"/></td></td>
</tr>
</table></td>
</form>
as the form.
when i try using print_r($_post); the output is
array()
which i believe means empty.
However, if i set the $type into $type="txt"
the output in database will be
txt
i can't seems to find the solution for this.
Please help me.
Any help will be very appreciated.
post array can't be accessed with $_post instead you should use $_POST to access post array
print_r($_POST)
Your input 'ref22' does not have a value.
<input type="text" id="ref22" name="ref22" value='xxx' size="20" />
^..........^
so the posted array will be empty.
Strip your form code down to bare minimum and test that your 'form' submits 'name:value's
<form action="insert_integrity.php" method="POST">
<input type="text" name="ref22" value='123' />
<input type="submit" name='submit' />
</form>
I'm trying to create an auto-response script for work which will display the form fields as required, such as Name, Username, and Password. Once I select Submit, I want the script to remove the form, and display the output with the corresponding response in its stead without having to post it to another page. Ultimately having a form.php and a result.php for each individual scenario that I want will be too cluttered, hence why I'm trying to accomplish this. Thus far for the form, I have:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table width="100%" cellpadding="10" cellspacing="0">
<tr>
<td style="width:33%">
</td>
<td style="width:33%"> </td>
<td style="width:33%"> </td>
</tr>
<tr>
<td colspan="3" style="width:99%">
<label>Name</label><span class="req">*</span>
<span id="reqName" style="color:Red;display:none;"> </span>
<br />
<input type="text" name="name">
</td>
</tr>
<tr>
<td colspan="3" style="width:99%">
<label>Username</label><span class="req">*</span>
<span id="reqProblem" style="color:Red;display:none;"> </span>
<br />
<input type="text" name="username">
</td>
</tr>
<tr>
<td colspan="3" style="width:99%">
<label for="txtProblem">Password</label><span class="req">*</span>
<span id="reqProblem" style="color:Red;display:none;"> </span>
<br />
<input type="text" name="password">
</td>
</tr>
<tr>
<td colspan="3" style="width:99%">
<br />
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
For the PHP I have:
<?php
if(isset($_POST['submit'])) {
echo "<h4>Response</h4>";
$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
echo "<p>Hello, $name <br />
Your Login Information is as follows:
Username: $username
Password: $password.
</p>";
}
?>
So to sum up, I want to completely remove everything before the PHP isset and replace it with the corresponding result.
In the present form what happens is that your HTML is sent off to the browser (or at least some invisible buffer on its way to the browser) and is out of your control once you've gotten to the part of your script that controls form processing.
So you've got two choices. One is to leave it generally in the state you have it but use output buffering with ob_start() and related functions - which I really don't recommend in this case - or just re-arrange the order of your code.
In general the most common design pattern for these one-page self-processing forms is to have your processing logic at the very top, and the output of form or success/failure message be conditional on the results.
To implement this take your PHP code and move it to the top of the file. At the end of your present POST code, if the post was successful then you output your message but nothing else. If it isn't successful, or if there has not yet been any post, THEN you output the form html. Something along these lines:
<?php
if($no_post_or_failure)
{
// output html
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
...
<?php
} // aaaand we're done!
?>
And that's about it. I hope that made sense :)
Just change your PHP:
<?php
if(isset($_POST['submit'])) {
echo "<h4>Response</h4>";
$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
echo "<p>Hello, $name <br />
Your Login Information is as follows:
Username: $username
Password: $password.
Back
</p>";
}else{
?>
//YOUR HTML FORM GOES HERE
<?php
}
So Im relatively new to PHP from ASP. And After converting alot of ASP code into PHP I have come across a problem where my PHP code seems unable to find the hidden variable I have set. It worked fine in ASP and was just wondering the best way to resolve this.
Start of the Form:
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
<table align="center" cellSpacing="2" cellPadding="2" border="0">
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">User name:</font> </td>
<td><input name="UserName" size="25" type="Text" autocomplete="OFF"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">Password:</font> </td>
<td><input name="Password" size="25" type="Password" autocomplete="OFF"></td>
<td> </td>
</tr>
PHP script:
<?
if ($_POST["BtnPress"]=="Pressed")
{
if ($_POST["Username"]=="*****" && $_POST["password"]=="*********")
{
$_SESSION['AdminID']="1";
header("Location: "."index.php");
}
else
{
print "<font color=#ff0000>Sorry you cannot access this part of the site.</font>";
}
}
?>
then the rest of the form:
<tr>
<td align="center" colspan="4">
<input type="hidden" name="BtnPress" value="Pressed">
<input type="Submit" value="Log In" class="mybutton" onclick="return CheckForm();">
</td>
</tr>
</table>
</td>
</form>
The PHP seems unable to find the variable BtnPress, its a similar problem throughout alot of my translated ASP to PHP script. Sorry if it is a simple solution but can anyone tell me where I am going wrong?
You have name="Password" and $_POST["password"]
Password != password
Watch your case.
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
That is invalid HTML. A <td> element cannot be a child element of a <form>. Browsers are likely to error recover in ways that break your HTML (e.g. by moving the form, but not its contents, outside the table). Do use a validator.
And stuff that isn't likely to be the cause of the problem, but is likely to be the cause of other problems.
Don't use layout tables
Do use the label element
Do use CSS for presentation (you can style your label elements instead of using the obsolete font element
Do not use a hidden input to test if a form is submitted.
use:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// processing of $_POST
}
The name of the submit button should be "BtnPressed" not the hidden field.
What I would do would be to set the form action to ?id=submit or something like that, and then check "if $_GET['id'] == "submit" then process data.
<form action="login.php?do=submit">
.... <input .....
</form>
<?php
if($_GET['do'] == "submit"){
//process data
} ?>
This is what I have done. I've modified the front-end uploaded for the SermonSpeaker module that our church is using to where I can connect our S3 Amazon cloud to store our sermon mp3s. The original front end form is 2 parts. The first part is uploading the mp3 audio which originally linked to the second part of the form where you can add the sermon title, scripture, etc... It would display the name of the file on the 2nd part of the form and once completed, would officially post the sermon, and have the path correctly routed in the sermon audio link. I used the tutorial for the S3 Amazon post that has worked like a charm, and I have been able to successfully redirect part 1 of the form to part 2, but the filename shows up as " .$filename', and the direct link shows up on the Joomla backend hows up as "/http://cpcpca-sermons.s3.amazonaws.com//'.$filename"
I also wanted it to reject any file types that are non-mp3s. The conditions feature of the base64 policy that I used don't seem to work as I've uploaded pdf files as a test, and they've gone through instead of being denied. I'm still a noob on most of this stuff, but I feel so close to a breakthrough. Here's a copy of the files that I've been working on.
This is the section that I used to call the redirect to step 2:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<table border="0">
<tr>
<td width ="50"></td>
<td><h1><?php echo JText::_('COM_SERMONSPEAKER_FU_NEWSERMON'); ?></h1></td>
</tr>
<tr>
<td width ="50"></td>
<td><b><?php echo JText::_('COM_SERMONSPEAKER_FU_STEP'); ?> 1 : </b><?php echo JText::_('COM_SERMONSPEAKER_FU_STEP1'); ?></td>
</tr>
<tr>
<td colspan ="4"> </td>
</tr>
<tr>
<td width ="50"></td>
<td align='left'>
<form action="http://s3.amazonaws.com/cpcpca-sermons" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="${filename}" />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="content-type" value="audio/mp3" />
<input type="hidden" name="redirect" value="http://www.cpcpca.org/index.php?option=com_sermonspeaker&view=fu_step_2&filename='.$filename" />
<input type="hidden" name="AWSAccessKeyId" value="REMOVED" />
<input type="hidden" name="policy" value="REMOVED"/>
<input type="hidden" name="signature" value="REMOVED" />
<!-- Include any additional input fields here -->
File to upload:
<input name="file" type="file">
<br>
<input type="submit" value="Upload Sermon">
Please upload mp3's only.
</form>
<br/> <br/>
<td>
</tr>
<tr>
<td width ="50"></td>
<td><?php echo SermonspeakerHelperSermonspeaker::fu_logoffbtn(); ?></td>
</tr>
</table>
You have to enclose the php variables in php tags: for them to be treated as php code, otherwise it is considered html. Also in the url you have to echo the variable http://www.cpcpca.org/index.php?option=com_sermonspeaker&view=fu_step_2&filename=<?php echo $filename ?>
My Plugin form code:
function pp_settings_page() {
$pp_options = get_option('pp_options');
?>
<div class="wrap">
<h2>Post Products Settings</h2>
<form action="options.php" method="post">
<?php settings_fields('pp_options_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Currency sign: </th>
<td><input type="text" name="pp_options[currency_sign]" value="<?php echo $pp_options['currency_sign']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save changes" />
</p>
</form>
</div>
<?php
}
I have tried to call it within the template files using:
<?php $pp_options=get_option('pp_options');?>
and
<?php get_option('pp_options');?>
What am I missing?
I don't see any code to handle the form submit, more specifically, code which extracts the post variable and saves it using update_option
Your action would need to be changed to the settings page URL so when it gets posted it runs the next bit of code, which you would put inside your pp_settings_page function.
if(isset($_POST['field_name'])) {
$field_value = $_POST['field_name'];
update_option('option_name', $field_value);
}