I am calling a console application from php which gives me some output. I have to display the output in a div after the console application is executed. I have passed the output from the console application as a SESSION to another page to display it. I have used ajax and javascript in my webpage too which make it more complicated. Now when I print the output the output in the div is the previous value of the SESSION.My code is as follows:
The main page:
<!DOCTYPE html>
<html>
<head>
$('#file').live('change', function()
{
$("#preview").html('');
$("#preview").html('<img src="images/loader.gif" alt="Uploading...."/>');
$("#imageform").ajaxForm(
{
target: '#preview'
}).submit();
<script type="text/javascript">
function myfunction()
{
$("#display").html('');
$("#display").html('<img src="images/loader.gif" alt="Uploading...."/>');
$("#retrieveimageform").ajaxForm(
{
target: '#display'
}).submit();
$('#tags').html('<?php include 'Tagsdisplay.php';?>');
%I want to include this only after it goes through the retrieveimage1.php page
where the console application is called and the session is created but this is
not happening the tags div is shown before the console application is called
and the other results are displayed on the display div
$('#tags').show();
};
</script>
<title></title>
</head>
<body>
<div id="maindiv">
<div id="banner">
<?php
include 'Header.php';
?>
</div>
<div id="wrapper">
<div id="imageupload">
<form action="uploadimage.php" method="post"
enctype="multipart/form-data" id="imageform">
<label for="file">Upload your image:</label>
<input type="file" name="file" id="file"><br>
</form>
</div>
<div id='preview'>
</div>
<div id='tags'>
</div>
<div class="clear"></div>
<form action="retrieveimage1.php" method="post"
id="retrieveimageform">
................................
<input type="submit" name="search" value="Search" id="btnSearch" onclick="myfunction()">
</form>
<div id="display"></div>
</div>
The retrieveimage.php where the console app is called and session is created:
session_start();
$start_time= microtime(true);
if (isset($_SESSION['img']))
{
$image=$_SESSION['img'];
$_SESSION['coarselbl1']=" ";
$_SESSION['coarselbl2']=" ";
$_SESSION['coarselbl3']=" ";
$_SESSION['finelbl1']=" ";
$_SESSION['finelbl2']=" ";
$_SESSION['finelbl3']=" ";
$_SESSION['finelbl4']=" ";
$_SESSION['category']=" ";
if($_POST['cat']=='handbag')
{
$_SESSION['category']="Bag";
$cwt=$_POST["slider1"];
$fwt=$_POST["slider2"];
$twt=$_POST["slider3"];
$swt=$_POST["slider4"];
$addr="handbags31_fourth.exe $image $cwt $fwt $swt $twt";
exec($addr,$data);
$_SESSION['coarselbl1']=$data[2];
$_SESSION['coarselbl2']=$data[3];
$_SESSION['coarselbl3']=$data[4];
}
The TagsDisplay.php where the tags are displayed:
<?php
session_start();
echo $_SESSION['category']."<br/>";
if($_SESSION['category']=="Bag")
{
echo "Coarse Color: ".$_SESSION['coarselbl1'].",".$_SESSION['coarselbl2'].",".$_SESSION['coarselbl3']."<br/>";
unset($_SESSION['coarselbl1']);
unset($_SESSION['coarselbl2']);
unset($_SESSION['coarselbl3']);
}
?>
The problem here is when I click the search button the myfunction is called and the tags div is displayed before the display div and the value printed are the previous session value. I want the tag div to come only after the display div is shown and the new session are assigned. How can I achieve that?
Fix the jquery line
$('#tags').html('<?php include 'Tagsdisplay.php';?>');
to this:
$('#tags').html('<?php include "Tagsdisplay.php";?>');
(note the quotes) because you are not even including the file that unsets the previous session's values.
Related
Good Day!
I am having a difficulty when it comes to showing a pop-up box in my webpage. I would like to show it when it meets a certain condition inside my php code, which is under the Condition.php. I have included the js file, which removes a certain class to make the box visible. How would I execute the JS code inside the Condition.php when it meets a certain condition?
Here are my codes:
Condition.php
<?php
// Defined variables and additional codes section
if (strlen($str) == 4) {
// Show the popup box
}
// Additional Codes
?>
ConfirmCheck.js
$(document).ready(function () {
$('#confirm').click(function () {
$('.popup').removeClass("hide");
});
});
Check.php
<form class="frm" action="Condition.php" method="POST">
// Additional Codes here
<input type="submit" name="checkOutBtn" value="CONFIRM" id="confirm">
</form>
<?php include 'box.php';?>
<script src='ConfirmCheck.js'></script>
Box.php
<div class="popup hide" id="popupID">
<div class="box">
<div class="form">
<h1>SUCCESS!</h1>
<form action="home.php">
<div class="form-group">
<p class="paragraph">
Your order has been successfully placed!
</p>
<button class="homepageBtn" onclick="home.php">GO TO THE HOME PAGE</button>
</div>
</form>
</div>
</div>
</div>
To do what you require simply put the if condition inside box.php and remove condition.php as it serves no purpose having an entire PHP page for a single condition.
box.php
<div class="popup <? if (strlen($str) != 4) { ?>hide<? } ?>" id="popupID">
<div class="box">
<div class="form">
<h1>SUCCESS!</h1>
<form action="home.php">
<div class="form-group">
<p class="paragraph">
Your order has been successfully placed!
</p>
<button class="homepageBtn" onclick="home.php">GO TO THE HOME PAGE</button>
</div>
</form>
</div>
</div>
</div>
I guess the problem is that you've set the action of your form to Condition.php but included the box design and code on check.php.
Note that #confirm is and input of type submit so after its pressed it will redirect you to the page specified at the action of the form.
I can suggest two possible fixes to that:
[least suggested] display the confirmation box on the Condition.php page
[most suggested] use AJAX!
The first fix requires you to move the markup and styles for box to the Condition.php file and design a whole confirmation/post action page
The second fix is better because by sending the Data to the server using AJAX you're not only going to stay on the same page (check.php) but you can also sort of hide the address to Condition.php which is supposed to be a backend file(from what i understood)
The structure should look something like this:
check.php:
<div class="frm">
// Additional Codes here
<buttin name="checkOutBtn" id="confirm">CONFIRMM</button>
</form>
<?php include 'box.php';?>
<script src='ConfirmCheck.js'></script>
ConfirmCheck.js:
$(document).ready(function () {
$('#confirm').click(function () {
// code to get all of your fields and put them in a js object: FDATA
$.ajax({type:'POST', url:'Condition.php', data: FDATA,
success:function(){
$('.popup').removeClass("hide");
}});
});
});
Condition.php:
<?php
// Defined variables and additional codes section
if (strlen($str) == 4) { //success
echo "success message";
}else{ // failed
header('HTTP/1.0 400 Bad Request');
die("bad request");
}
// Additional Codes
?>
The request goes back and forth between check.php and Condition.php in the background and your code gets notified through a callback whether or not to show the box.
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...
I have a select in my HTML form:
<form name="correo" id="correo" method="post" action="#" enctype="multipart/form-data" onSubmit="<!--return checkFields();-->" ><div class="multi-field-wrapper" name="multi-field-wrapper">
<div class="multi-fields" name="multi-fields">
<div class="multi-field" name="multi-field">
<div>
<label for="penviadas[]"> Cantidad </label>
<input type="number" name="penviadas[]" class="penviadas" id="penviadas" maxlength="70" placeholder="¿Cuántas?" onClick="removerIcon('iconcant');" >
</div>
</div>
</div>
<button type="button" class="add-field">Añadir otra referencia</button>
</div>
And I can add fields dynamically (or what's the same, I can repeat the code above many times; the code below works).
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
Let's say I have 3 different selects (penviadas). What I want is to get all of them in my PHP file once I submit the form. It used to work, but now, for some reason, I can only get THE FIRST select (penviadas). Why am I not getting all the values from penviadas array?
PD: I print it in my PHP in different ways but they all return ONLY THE FIRST ELEMENT from penviadas, not the rest:
var_dump($_REQUEST['penviadas']);
I figured it out after many hours playing stupid.
With the information in the OP it was impossible to discover where there was a problem. I found the solution here: Submitting form from different <div> HTML
Basically, I had this structure:
<div...
<form...
</div...
</form>
I thought it was alright and didn't think for a moment this could be affecting. Thus, it wasn't the PHP/JS but the html tags that were incorrect. Thanks for your time.
PHP Code :
<?php
if(isset($_POST['test']))
{
$data = $_POST['referenciasnuevas'];
foreach ($data as $key => $value) {
echo $value . "<br />";
}
}
?>
Html Code:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<form method="post" action="#">
<div class="multi-field-wrapper" name="multi-field-wrapper">
<div class="multi-fields" name="multi-fields">
<div class="multi-field" name="multi-field">
<label for="referenciasnuevas[]">Referencia pieza</label>
<select name="referenciasnuevas[]" id="referenciasnuevas" class="referenciasnuevas" style="width: 105px" onClick="removerIcon('iconref');">
<option selected value='-1'> ¿cliente? </option></select>
</div>
</div>
<button type="button" class="add-field">Añadir otra referencia</button>
</div>
<input type="submit" name="test" value="Submit">
</form>
<script>
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
</script>
</body>
</html>
The above code works for me.please look my code
I have two php pages. In first php page I have two divisions, where in one division I have hyperlinked text which on click showing result in other division of same page, with the help of ajax. The code for same is below:
<body>
<div id="container">
<div id="content"> Sidebar <p> </p>
<div class="form">
<pre>
<a href=sample_disease_form.php><b>Disease</b></a><p>
<a href=sample_drug_form.php><b>Drug</b></a><p>
</pre>
</form>
</div>
</div>
<div id="sidebar">
</div>
</body>
Ajax code for this is:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"
type="text/javascript"></script>
<script>
$(document).ready(function() {
$('a').each(function(){
$(this).on("click",function(e) {
console.log(e);
e.preventDefault();
$('#sidebar').load($(this).attr('href'));
});
});
});
</script>
Now, I have other PHP file, one which is opening after clicking hyperlink on same page but in other division, contains form. After being clicked submit button of this form I want the result gets displayed in same division but it will come from different PHP file. How can I achieve this?
The second file's code is below:
<pre><h2> Drug </h2></pre>
<pre><p><span class="error"> * required field </span></p></pre>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<pre> Name: <input type="text" name="drug" value="<?php echo $drug;?>"><span class="error">* <?php echo $nameErr;?></span>
<input type="checkbox" name="drug[]" value="disease">Disease</br>
<input type="checkbox" name="drug[]" value="target">chemical
<input type="submit" name="submit" value="Submit"></pre>
</form>
Since I am very new to these kinds of programming stuff, expecting help.
you need to submit the page through ajax and load the result in the same div
Following on from a previous question, (previous question here), the problem I'm having seems to involve trying to pass/post a value through a form when the form action is '#'. I've tried session data but it always returns the last item from the database. Everthing else returns nothing.
Any help/ideas/advice greatly received, S. (Code below)
This is the code that displays the list of items, each containing an 'email' link/button to one instance of a popup window/form that is located at the bottom of the page.
<?php
$query = mysql_query("select * from istable where categoryID = '1'");
while ($result = mysql_fetch_array($query)) {
echo '<h4>'.$result['title'].'</h4>
<p>'.substr($result['descrip'],0,408).'... <strong>Read more</strong></p>
<form action="#" method="post" rel="#sheet" class="see">
<input type="hidden" name="propTitle" value="'.$propResult['title'].'">
<input type="submit" name="submit" value="Email">
</form>
';
}
?>
This is the code for the popup window/form at the bottom of the same page that is called through jquery.
<div id="sheet" class="rounded">
<!--{{{ pane1 -->
<div class="pane" id="pane1">
<h4>Email Details to a Friend</h4>
<p>You have selected to forward the details of <?php echo $_POST['propTitle']; ?> to a friend.</p>
<p>Please fill out the following form</p>
<form class="rounded" id="email-form" method="post" action="<?php echo $pageLink; ?>">
<!-- form goes in here -->
</form>
</div>
<!--}}}-->
</div>
<script type="text/javascript">
$(".see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
$(".error").hide();
});
</script>
Why are you using PHP for this? If the popup is called through the same page, use JavaScript to get the DOM element value and if you need to process data use AJAX.