Passing result values from one PHP page to another - php

I've a form in which I send the values to be calculated to another PHP page which should do the calculation and return the values to this form page. If there is an error, it should return the error to this main page. How would I do that?
This is the basic form page: index.php:
<form id="deps" name="deps" action="calc.php" method="POST">
<div id="er"> <span class="er" > </span></div>
<table class="table">
<tr>
<td class="field head"> </td>
<td class="field cv"><label for="met">Metric</label></td>
</tr>
<tr>
<td class="field head"><label for="bd">Bore Dia.</label></td>
<td class="field"><input type="text" name="bdmm" id="bdmm" /><label for="bdmm">MM</label></td>
<tr>
<td class="field head"><label for="tw">Total Weight</label></td>
<td class="field"><input type="text" name="twkg" id="twkg" /> <label for="twkg">KG</label></td>
</tr>
</tr>
</table>
<input type="submit" id="submit" value="Calculate" />
</form>
</div>
Calc.php
<?php
$bdmm = $_POST['bdmm'];
if(!$bdmm){
$error = "Please enter a value!";
echo $error;
}
else {
echo $bdmm ." success";
}
?>
If there is an error it should display the error in the span tag. And if there is no error and it calculates, it should return the result and display it in the textbox.
Is this possible without Javascript?

Sure:
index.php:
<?php
$err = (isset($_GET['error'])) ? $_GET['error'] : ' ';
?>
<form id="deps" name="deps" action="calc.php" method="POST">
<div id="er"> <span class="er" ><?php echo $err; ?></span></div>
....rest of form
calc.php:
if(empty($_POST['bdmm'])){
header('Location: index.php?error=Please enter a value!');
exit();
}
else {
echo $_POST['bdmm'] ." success";
}
While this works just fine, I'd recommend you simply post the form to the same page and avoid the url redirection. If you use an MVC framework such as CodeIgniter or Zend, you can take advantage of unique urls within the same file and tell the controller to load the appropriate view while keeping the request on the same file.

Related

Jquery empty field add message beside the field

Jquery:
$(document).ready(function(){
$("#login_form").submit(function(event){
var username = $.trim($("#username").val());
var password = $.trim($("#password").val());
if(username.length < 1){
if(!$("#user_error").length){
$("#username_error").append("<p id='user_error'>Required</p>");
}
}
if(username.length < 1|| password.length < 1){
$("#username_error").append("<p id='user_error'>Required</p>");
$("#password_error").append("<p id='pass_error'>Required</p>");
event.preventDefault();
}
});
});
View:
<? $attr = array( "id" => "login_form"); echo form_open("KGLogin/verify_login",$attr); ?>
<table id="loginform">
<tr>
<th>Log-in</th>
</tr>
<tr>
<td>
<input type="text" name="username" id='username' placeholder="Username" />
</td>
<td id="username_error"></td>
</tr>
<tr>
<td>
<input type="password" name="password" id='password' placeholder="Password" />
</td>
<td id="password_error"></td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Login" />
</td>
</tr>
</table>
<? echo form_close(); ?>
Is there a simpler way to do this? IF i submit it twice without inputting anything, it will continue to append. Im a newbie to jquery, Cant seem to find a simple way to do things, all search results in google directs me to complicated jqueries.
How do i keep the function from stacking the error message.
Ill just put this as an answer, so no one would suggest.
<?if(form_error('username'))echo "required";;?>
Put the following code in autoload file.
$autoload['helper'] = array('url','array','html','form');
Then try this in view file
<?php if (form_error('your field name'))
{
echo "required";
}
;?>

How To Blank The Page Before Submitting PHP Form To Same Page?

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
}

Having Trouble Calling WordPress Plugin Form Value

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);
}

PHP session problem

I am using session_start() function in config.php file and this file is included in all files. in my add_page.php page, i am submitting a form and saving a message(in submit action) in the session, and then re-directing to index.php page(using header('location:...')). but after re-directing, i am not getting the message from the session. the other session data exist like-username, logged_in, etc. any idea???
Code bellow(add_news.php). after submitting form, it re-directs to news.php. config.php file has session_start() function at the top and it is included in every page:
<?php
include('../include/config.php');
//submit edit data
if (isset($_POST['submit_edit'])) {
$news_head = stripslashes(trim($_POST['news_head']));
$news_details = $_POST['news_details'];
$news_special = ($_POST['news_special']=='')?'0':'1';
$news_status = ($_POST['news_status']=='')?'0':'1';
$data = array('news_head'=>$news_head,
'news_details'=>$news_details,
'news_special'=>$news_special,
'news_status'=>$news_status);
$clause = "news_id = '".$_POST['news_id']."'";
$response = update_data('news', $data, $clause);
if($response)
$_SESSION['success_msg'] = "News updated successfully.";
}
//submit edit data end
?>
<form action="add_news.php" method="post" id="add_news_form">
<input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
<table width="100%" border="0" cellspacing="4" cellpadding="4">
<tr>
<td>News Heading</td>
<td><input type="text" name="news_head" id="news_head" class="required" value="<?php echo $news_head ?>" size="100" /></td>
</tr>
<tr>
<td>News Details</td>
<td><textarea name="news_details" id="news_details" class="editor required" rows="7" cols="60"><?php echo trim(stripslashes($news_details)); ?></textarea></td>
</tr>
<tr>
<td>Special News</td>
<td><input type="checkbox" name="news_special" id="news_special" value="1" <?php if($news_special=='1') echo 'checked="checked"'?> />
[ Checked means special ]</td>
</tr>
<tr>
<td>Status Status</td>
<td><input type="checkbox" name="news_status" id="news_status" value="1" <?php if($news_status=='1') echo 'checked="checked"'?> />
[ Checked means published ]</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" <?php if($_REQUEST['news_id']!="" && $_REQUEST['action']=='edit') echo 'name="submit_edit"'; else echo 'name="submit"'; ?> /></td>
</tr>
</table>
</form>
Make sure the call to session_start() occurs before any calls to save a message in the session. session_start() has to be called on every page request that needs the session, and has to occur at the very beginning of the page, even before the submit action occurs.
Edit
See http://bugs.php.net/bug.php?id=14636 for more information and some suggestions to try.

Need validation for a form in PHP not in Javascript?

Hai.. i already added javascript validation for my form.Its working fine.But mean while i ve to validate the same form using PHP also.
This is the form code:
class airportclass{
function add_airport_frm(){
$errmsg=0;
<table>
<form action="" method="post" name="airport" >
<tr> <td colspan="2" align="center"><?php echo $errmsg; ?></td></tr>
<tr>
<td><strong>Code :</strong></td>
<td><input type="text" name="code" id="code" /></td>
</tr>
<tr>
<td><strong>Name:</strong></td>
<td><input type="text" name="name" id="name" /></td>
</tr>
</form>
</table>
}
This is the PHP script in another file :
if(isset($_POST['postcode']) && $_POST['postcode']!="")
{
$code=$_POST['code'];
$name=$_POST['name'];
$postcode=$_POST['postcode'];
$auth=$_POST['auth'];
if(trim($code)=='')
{
$errmsg = 'Please enter code';
}
}
But its not working.
Only javascript validation is working.PHP validation is not working.
Can any one suggest me????
As the form action="" is blank, the form is being posted to the same file. You indicated that you have your validation script in another file!
The $errmsg variable you're using is not global. It needs to be global if you intent to use it inside multiple functions.
You are checking against 'postcode', but postcode is nowhere set in your form.

Categories