Safe way to update DIV with other page-content - php

I'm wondering what is the best and safest way to make a redirect in a div after submitting a form.
So, I have a form with a calulation inside a div. When the calculation is 0 I would like to show page_zero.php into this div instead of the form.
Here is some code:
form_page.php
<div class="form_wrapper">
<div id="PP_succes_engine" style=""></div>
<form id="SignupForm" action="" method="post" ONSubmit="xmlhttpPost('invoice_engine.php', 'SignupForm', 'PP_succes_engine', '<div class=\'PP_wait_loading\'></div>');return false; ">
<?php
//calculation
?>
<input name="tot_price" type="hidden" value="<?php echo $tot_price; ?>">
<input name="Submit" type="submit" value="Submit" class="type-button">
</div>
invoice_engine.php
<?php
session_start();
if ($_POST['tot_price'] == 0) {
// REDIRECT ME TO 'page_zero.php' BUT STAY INTO <div class="form_wrapper">
}
else {
// RUN OTHER SCRIPT
}
?>

if ($_POST['tot_price'] == 0){
include 'page_zero.php';
exit;
else ...
You can just include. Why redirect?

Life would be simpler and cleaner to implement using jQuery:
Have a read through: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Not exactly what you're looking for but all the elements are there.
Basically, you can call your invoice_engine.php and get it to return HTML that you can then 'inject' into a DIV on your page.

Related

How to exclude/remove an input after the php code is executed.?

So i have an post input where i submit data something simple.
<form method="post" action="result.php">
<input type="url" name="url" class="form-control" placeholder="http://example.com/">
<input type="submit" name="submit" />
</form>
After the html code is going to be executed a php code which echo success or something like this that doesn't matter.
But i have a problem when i include('submit.php') it's going to show also the input and i don't want this.
How i can do that to don't show the input on result.php?
If you want it to be user-specific, you can try to use cookies or sessions like this:
index.php
<?php
session_start();
?>
<?php if(!isset($_SESSION['show_button']) && !$_SESSION['show_button'] ){ ?>
<!-- Button logic here... -->
<?php } ?>
result.php
// If the url has been entered, it returns a false from empty()
$_SESSION['show_button'] = empty($_POST['url']);

PHP/HTML form-action attribute

I have a small problem. I have one form, for search. After submitting this form is can't stay on same page.
My url:
www.localhost/index.php?vehicletype=car
On this url, the car search is visible.
After the submit I get this:
localhost/index.php?body_type=any&fuel_type.....
But I want this,
www.localhost/index.php?vehicletype=car?body_type=any&fuel_type....
I tried $_SERVER["REQUEST_URI"] but nothing. Thanks for the help, and sorry for my English grammar!
EDIT: The code:
<? if($_GET['vehicletype']=='car'){ ?>
<div class="search">
<form method="get" action="">
....
<button class="btn btn-search" type="submit">Search <hr> (658 found)</button>
</form>
</div>
First thing following url is wrong
"www.localhost/index.php?vehicletype=car?body_type=any&fuel_type...."
after "?" it will set all argument in get method you can access that variable using $_GET['variablename']
if you want to be on same page you can action like following way
<form action="#" method="GET">
When someone click on submit button same page will be called with form params.
You can get all form param by using $_GET method.
Method 1.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
else
{
?>
Show HTML Form here
<?php } ?>
Method 2.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
<form action="#" method="GET">
<input type="text" name="vehicletype" value="<?php if (isset($_GET['vehicletype'])) { echo $_GET['vehicaletype']; } ?>">
</form>
Let me know if you have further query.

Using forms from a tabbed index page

So I'm new to PHP and I'm having trouble getting some of my forms to function. I think it may be the way my site is set up that's causing me problems.
I have index.php which is set up as such:
<div class="pageContent">
<div id="main" style="width:1000px; margin:0 auto;">
<!-- Create the tabs -->
<div id="tabs" >
<ul>
<li>Overview</li>
<li>Ranked</li>
<li>Arena</li>
<li>Decks</li>
<li>New Game</li>
<li>Admin</li>
</ul>
<div id="tabs-0"></div>
<div id="tabs-1"></div>
<div id="tabs-2"></div>
<div id="tabs-3"></div>
<div id="tabs-4"></div>
<div id="tabs-5"></div>
</div>
<!-- Load the pages into tabs -->
<script>
$("#tabs").tabs();
$("#tabs-0").load("tab0.php");
$("#tabs-1").load("tab1.php");
$("#tabs-2").load("tab2.php");
$("#tabs-3").load("tab3.php");
$("#tabs-4").load("newGame.php");
$("#tabs-5").load("admin.php");
</script>
</div><!-- / main -->
</div><!-- / pageContent -->
This gives me a nice static page and 6 tabs of .php files to do cool stuff on.
There are a couple of forms, log in and such, in index.php which all function fine. But when I create a form on a page in a tab, it will not.
Here's an example from admin.php (#tabs-5)
<?php
if(isset($_POST['delLog'])){
unlink('log.txt');
echo 'Success';
}
if(isset($_POST['delLog'])){
unlink('error_log');
echo 'Success';
}
?>
<html>
<head>
</head>
<body>
<table width="100%" border="1">
<tr>
<td width="40%" valign="top">
</td>
<td width="30%" valign="top">
<h1>error_log</h1>
<form action="" method="post">
<input type="submit" name="delErr" id="delErr" value="Delete" />
</form>
<hr />
<?php
$lines = explode("\n", file_get_contents('error_log'));
foreach ($lines as $line){
echo $line.'<br>';
}
?>
</td>
<td width="3'0%" valign="top">
<h1>log.txt</h1>
<form action="" method="post">
<input type="submit" name="delLog" id="delLog" value="Delete" />
</form>
<hr />
<?php
$lines = explode("\n", file_get_contents('log.txt'));
foreach ($lines as $line){
echo $line.'<br>';
}
?>
</td>
</tr>
</table>
</body>
</html>
This is another, better example. A stripped down test I did yesterday for this question
Problems with $_POST
<?php
define('INCLUDE_CHECK',true);
include 'php/functions.php';
error_reporting(E_ALL);
ini_set('display_errors',1);
logThis('ready!');
if (isset($_POST['submit'])) {
logThis('success');
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>
Neither of these examples work. When the submit button is pressed the site refreshes but no PHP actions are taking place. There is no error message in error_log. I don't think it's getting the call at all.
Neither of the forms return an output, one adds to the database. The other deletes log files.
Hope I've provided enough details.
What ever you are trying to do is not possible at all. You can not load PHP code to frontend, it can only be processed by the server.
The statement $("#tabs-5").load("admin.php"); is fetching only the html code (processed by the server) not the PHP script
<form action="" method="post"> will post to the current page (ie. whatever is in your address bar).
When you load the pages into index.php using ajax, that means the forms will post to index.php... I'm guessing you are expecting to fetch the form submissions in each individual php-file
So to fix it either you have to also post using ajax (and refresh the tabs with the response), or you need to change the action attribute on the forms to each individual php-file... in which case you lose your tabbed layout when a form is submitted. You can work around that as follows:
In each of the php-files you do something like this (using newGame.php as example):
<?php
if (isset($_POST)) {
//Do your form-handling stuff first
// [...]
// ...and then redirect back to index.php
header("Location: index.php");
die();
} else {
//Print your form
echo '<form action="newGame.php" method="post">';
// [...]
echo '<input type="submit" value="Create game">';
echo '</form>';
}
Note that there should be no <html>, <head>, <body> or other html in the "sub-pages" at all - just the bare content you want inside the tabs...
Now for the ajax-submit method you should also remove all "wrapping html", you should still set a target="[...]" on each form, and you should still do form handling inside each individual file. But you should not do a redirect after form handling, but just output the form again:
<?php
if (isset($_POST)) {
//Do your form-handling stuff first
// [...]
//Maybe print a little message to let the user know something happened
echo 'Form completed at ' . date('H:i:s');
}
//...then print your form no matter what
echo '<form action="newGame.php" method="post">';
// [...]
echo '<input type="submit" value="Create game">';
echo '</form>';
Then add this script to index.php - it will post all forms inside the tabs using ajax instead of refreshing the page:
<script>
my_cool_ajax_post = function(form_elm, tab_elm) {
//Actual ajax post
$.post(form_elm.target, $(form_elm).serialize(), function(data){
//Results are in, replace tab content
tab_elm.html(data);
});
};
$(document).on("submit", "#tabs form", function(){
//Store the form element
var form_elm = this;
//Find the parent tab element
var tab_elm = $(this).closest('#tabs > div');
//Really simple "loader"
tab_elm.html('<p>loading...</p>');
//Submit by ajax
my_cool_ajax_post(form_elm, tab_elm);
return false;
});
</script>
Disclaimer: Completely untested, so let me know...

submit form to page and depending on input show different div

I Have a form which when submitted needs to go to the page and then show one of 4 hidden divs depending on the page.
Here is the form
<form>
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Here is the page
<div id="brighton">
<p>Brighton</p>
</div>
<div id="devon">
<p>Devon</p>
</div>
<div id="search">
<p>search</p>
</div>
<div id="variety">
<p>variety</p>
</div>
So if Brighton is typed into the place input i need the form to submit the page and show the Brighton div and if Devon is typed in to show the Devon div etc and if the 2/12/2012 is typed into the date picker input and Brighton into the place input it goes to the page and shows the variety div.
i also need it so if the 1/12/2012 is typed in to the date picker input the page redirects to the page show.html.
any help would be greatly appreciated
thanks.
This is easy if you know PHP at all. It looks like you need a good, easy start. Then you will be able to achieve this in seconds.
Refer to W3SCHOOLS PHP Tutorial.
To achieve what you have mentioned, first make the following changes in your form:
<form action="submit.php" method="post">
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Create a new file called submit.php and add the following code:
<?php
$place = $_POST['place'];
$date = $_POST['datepicker'];
if ($date == '1/12/2012') {
header('Location: show.html');
exit;
}
?>
<?php if ($place == 'Brighton''): ?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php elseif ($place == 'Devon'): ?>
<div id="devon">
<p>Devon</p>
</div>
<?php elseif ($place == 'search'): ?>
<div id="search">
<p>search</p>
</div>
<?php elseif ($place == 'Variety'): ?>
<div id="variety">
<p>variety</p>
</div>
<?php endif; ?>
Now the above example is not the complete solution, but it gives you an idea as to how you can use if-then-else construct in PHP to compare values and do as desired.
Post your form to a php page and then check the posted form parameters to determine which div to show.
<?php
if ($_POST["place"] == "Brighton") {
?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php
} else if ($_POST["place"] == "Devon") {
?>
<div id="devon">
<p>Devon</p>
</div>
<?php
}
?>
Do that for each div and parameter combination. Make sure you set the "method" attribute on your form to "post":
<form action="somepage.php" method="post">...</form>
In the resulting HTML you will only see the one that matches the form parameter.

Prevent PHP from running until POST

So I'm really really really new to PHP and MySQL (just started today!).
I have a page that updates a MySQL database based on results from a post.
The way I have it now, the PHP code is on the page, and it works, but the problem is that it goes through on page load as well, that is, before the POST happens.
Any way to prevent this?
Here's my code:
<?php
//do stuff
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
while($row = mysql_fetch_row($result)) {
//do something
}
}
else {
//do something else
}
// free result set memory
mysql_free_result($result);
$first = 0;
?>
<br />
<br />
<div id="addName">
<h3 class="caps">Want to add yourself?</h3>
<div class="box">
<br /> <br />
<form id="form1" name="form1" method="post" action="#">
Email:
<label>
<input type="text" name="email" id="email" />
</label>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
</div>
</div>
</div>
See, problem is that it will run //do something else before the form is filled out. Any suggestions?
Thanks!
Better Condition
if(isset($_POST['submit']) && !empty($_POST) ){
// now do with your post data
}
this will prevent empty data to be posted
if(isset($_POST['submit'])){
// do things when post has been made
// e.g. insert the data into SQL
}
Take a read of this page. The above code will check to make sure the submit button has been pressed.
If you are hosting the HTML form on a different page you can put this at the top of your code on the PHP page. It will redirect the user to the HTML form file before any code is executed.
if (!$_POST['submit']) {
header("Location: ./form.html");
die();
}
However if you prefer to keep the form on the same page (which it looks like you are) you can use the suggestions above.

Categories