Session variable to show a form or not - php

I want to show a form only if the previous form is filled and submitted. So, I created a session variable which is initialized when I submit the first form. Like this :
if( $flag)
{
$_SESSION['id_essai'] = $id_essai;
echo $_SESSION['id_essai'];
insertion_base($index_essai,$id_essai,$nom_local_essai,$nombre_traitements,$essai_ou_suivis,$thematique);
}
I can get this var in this file because my echo works and return the value.
After that :
if(isset($_SESSION['id_essai']))
{
echo'
<div id="tab_traitement" class="excel"></div>
<form action="#" method="POST">
<button type="button" class="btn btn-default" id="submit_button_traitement">Submit</button>
</form> ';
}else
echo'Pour saisir un traitement, vous devez d\'abord faire la saisie d\'un essai';
This is my Index.php :
<?php session_start();?>
<!DOCTYPE HTML>
<meta charset="UTF-8">
<head>
<title></title>
</head>
<body>
<?php include('onglets.php');?>
</body>
In the file 'onglets.php' : I've got :
<div class="tabs" id="tabs">
<ul>
<li>Essai</li>
<li>Traitement</li>
<li>ITK PRO</li>
<li>ITK </li>
<li>Parcelle élémentaire</li>
<li>Prélèvement</li>
<li>Analyse</li>
<li>Mesure plante principale</li>
<li>Mesure plante CIPAN</li>
<li>Station Climatique</li>
</ul>
<div id="tabs-1">
<?php include('essai.php');?>
</div>
<div id="tabs-2">
<?php include('traitement.php');?>
</div>
<div id="tabs-3">
<?php include('itk_pro.php');?>
</div>
<div id="tabs-4">
<?php include('itk.php');?>
</div>
<div id="tabs-5">
<?php include('pe.php');?>
</div>
<div id="tabs-6">
<?php include('prelevement.php');?>
</div>
<div id="tabs-7">
<?php include('analyse.php');?>
</div>
<div id="tabs-8">
<?php include('mpp.php');?>
</div>
<div id="tabs-9">
<?php include('mpc.php');?>
</div>
<div id="tabs-10">
<?php include('station_climatique.php');?>
</div>
</div>
<div id="tabid"></div>
And I call ajax_insert_essai.php in essai.php. In ajax_insert_essai.php, I initialize $_SESSION['id_essai'].
And I have my session_start() at the beginning without something before so that's not the probem. Please, some help!
How can I do ?

This is because you use ajax. Javascript is client side and php is server side.
The page with the form is not recharged, so it isn't processed by the server, so you won't be able to see the session variable unless you refresh the page
Two solutions :
Refresh the page after submitting the first form or use a client-side method, with javascript (better imo)

Related

How to dynamically change HTML content of a page with information from a PHP file?

So, I have a php file called db.php to replicate some sort of database. This is basically a file with some multidimensional associative arrays:
<?php
$arrShowcases = array(
"1"=>array("title"=>"Landing Page", "descr"=>"Welcome!", "img"=>"actions-landing.jpg", "polygon"=>array(
0=> array(
"points"=>"1338,4,1338,50,1272,50,1272,4",
"link"=>"69",
"modalbody"=>"This button lets you Signup on the webapp",
"modaltitle"=>"Signup Button"
),
1=> array(
"points"=>"1246,12,1249,44,1206,44,1204,14",
"link"=>"2",
"modalbody"=>"This button redirects you for the login form",
"modaltitle"=>"Login Button"
)
)),
"2"=>array("title"=>"Login page", "descr"=>"Make your login", "img"=>"actions-login.jpg", "polygon"=>array(
0=> array(
"points"=>"1338,4,1338,50,1272,50,1272,4",
"link"=>"69",
"modalbody"=>"This button lets you Signup on the webapp",
"modaltitle"=>"Signup Button"
),
1=> array(
"points"=>"1246,12,1249,44,1206,44,1204,14",
"link"=>"69",
"modalbody"=>"This button redirects you for your dashboard",
"modaltitle"=>"Login Button"
)
))
);
?>
Then, I have a html page with a section element where I need to change page header title, subtitle, image and some polygon points:
<section>
<div class="container">
<div class="row">
<div class="col-md-3">
<?php include cfgPath."/includes/menu.html"; ?>
</div>
<div class="col-md-9">
<h1 class="page-header"><i class="glyphicon glyphicon-picture"></i> Landing page</h1>
<h3>// Lorem ipsum...</h3>
<div class="col-md-12">
<div style="position:relative;">
<img id="pageScreenshot" src="<?php echo cfgRoot; ?>/assets/images/actions-landing.jpg" class="img-responsive" alt="Landing Page"/>
<svg id="svgImageMap" width="1366" height="768">
<polygon points="1338,4,1338,50,1272,50,1272,4" data-originalpoints="1338,4 1338,50 1272,50 1272,4" data-id="0"/>
<polygon points="1246,12,1249,44,1206,44,1204,14" data-originalpoints="1246,12 1249,44 1206,44 1204,14" data-id="1"/>
<polygon points="378,43,446,42,445,11,377,9" data-originalpoints="378,43 446,42 445,11 377,9" data-id="2"/>
<polygon points="196,10,367,10,366,42,195,43" data-originalpoints="196,10 367,10 366,42 195,43" data-id="3"/>
<polygon points="121,16,120,35,164,39,164,17" data-originalpoints="121,16 120,35 164,39 164,17" data-id="4"/>
<polygon points="14,15,13,40,95,43,95,12" data-originalpoints="14,15 13,40 95,43 95,12" data-id="5">
</svg>
</div>
<!-- data original points: X1,Y1 X2,Y2 X3,Y3 X4,Y4 -->
</br>
<a class="btn btn-success btn-sm btn-block" href="<?php echo cfgRoot; ?>/app/showcase/showcaseView.php">Go Back</a>
</div> <!-- colmd12 -->
</div> <!-- colmd9 -->
</div> <!-- row -->
</div> <!-- container -->
</section> <!-- section -->
As you can see on the above html for the section element, I wrote by hand all the information. However, my goal is to change that info with stuff provided from the db.php file.
I want something like this nameofthepage.php?id=1 (and all the info provided in the position 1 of the array goes to the html) or this: nameofthepage.php?id=2 (and all the info provided in the position 2 of the array goes to the html). Any advice or tip to get this behavior?
So far, I tried to do echos like this:
<?php echo $arrShowcases[positions-that-i-want]["information-that-i-want"]; ?>
to change the hardcoded html but that doesn't get me the dynamic behavior I need.
I hope you'll get the point.
<?php
//-----------------------------
// If id is not sent then stop with page execution. You can redirect or something else.
if(!isset($_GET["id"])) {
die("ID not received.");
}
// Get that received id because at this point we are sure that id is received because page execution did not stopped before.
$id = $_GET["id"];
//-----------------------------
// Import your "database".
require_once "db.php";
//-----------------------------
// If there is no page with that id, stop page execution.
if(!isset($arrShowcases[$id])) {
die("page does not exists.");
}
// If there is a page then store it in a variable.
$page = $arrShowcases[$id];
//-----------------------------
?>
<section>
<div class="container">
<div class="row">
<div class="col-md-3">
<?php include cfgPath."/includes/menu.html"; ?>
</div>
<div class="col-md-9">
<h1 class="page-header">
<i class="glyphicon glyphicon-picture"></i>
<?= $page['title']; ?> // Echo page title.
</h1>
<h3><?= $page['descr']; ?></h3> // Echo page description.
<div class="col-md-12">
<div style="position:relative;">
<img id="pageScreenshot" src="<?php echo cfgRoot; ?>/assets/images/<?= $page['img']; ?>" class="img-responsive" alt="<?= $page['title']; ?>"/> // Echo image filename and page title.
<svg id="svgImageMap" width="1366" height="768">
// For each polygon, echo it's points and key as an index (0, 1, 2, 3...)
<?php foreach ($page["polygon"] as $key => $value) { ?>
<polygon points="<?= $value['points']; ?>" data-originalpoints="<?= $value['points']; ?>" data-id="<?= $key; ?>"/>
<?php } ?>
</svg>
</div>
<!-- data original points: X1,Y1 X2,Y2 X3,Y3 X4,Y4 -->
</br>
<a class="btn btn-success btn-sm btn-block" href="<?php echo cfgRoot; ?>/app/showcase/showcaseView.php">Go Back</a>
</div> <!-- colmd12 -->
</div> <!-- colmd9 -->
</div> <!-- row -->
</div> <!-- container -->
</section> <!-- section -->
When you enter an URL in the browser, you are making a GET HTTP request.
That request can take variables in the form of parameters, by adding ? to the end of your URL and then all the parameters you need, separated by &, like this(based on your example):
nameofthepage.php?id=1&another=2
In PHP, you can catch those parameters using the superglobal $_GET, like this:
$id = $_GET['id'];
By now you should understand #Spectarion answer, that solves your problem.
I don't think any answer should be marked as correct, because these are the basics of working with HTTP with PHP, but that is up to you.

How use Dynamic hyperlink on each wordpress post?

I built a static button from backend but i want that button get hyperlink from post editor
I have designed a static design and button on singlepost.php file now i want that when i create a new post then i want to put a redirect hyperlink behind that button from post editor section. how can i do that ?
how i set hyperlink behind that "Get Deal" button from post page ?
Get Deal button is static for all post but this button take link when i put from post editor section (so there will be separate dynamic link for each post)
My static design code for post is :
<div class="price_row_grid">
<div class="price_for_grid floatleft">
<?php theme_create_btn('no', 'price'); ?>
</div>
<div class="floatright vendor_for_grid">
<?php if ($price_meta == 'admin'): ?>
<?php $author_id = $post->post_author; ?>
<a class="admin" href="<?php echo get_author_posts_url($author_id) ?>" title="<?php the_author_meta('display_name', $author_id); ?>">
<?php echo get_avatar($author_id, '22'); ?>
</a>
<?php elseif ($price_meta == 'store'): ?>
<div class="brand_logo_small">
<?php WPSM_Postfilters::re_show_brand_tax('logo'); //show brand logo?>
</div>
<?php endif; ?>
</div>
</div>
<div class="price_row_grid">
<div class="price_for_grid floatleft">
<p class="button">Get Deal</p>
</div>
Now how i set hyperlink behind "Get Deal" from post editor because i just build button which is static for all pages but i want to set different hyperlink for each page.
you can take a help from this example
<html>
<head>
<script type="text/javascript">
function onsubmit_handler(){
var myForm = document.getElementById('myForm');
var data = document.getElementById('data').value;
if(data == "a")
myForm.setAttribute('action', 'a.php');
else if(data == "b")
myForm.setAttribute('action', 'b.php');
else
myForm.setAttribute('action', 'error.php');
}
</script>
</head>
<body>
<h3>Enter Text:</h3>
<form id="myForm" method="post" onsubmit="onsubmit_handler()">
<input type="text" id="data" name="data" value="">
<input type="submit" value="Post">
</form>
</body>
</html>

jQuery functionality lost in all items except the first, when using a loop

In the following page, begins an element that enables user to crop and save an image. It only works for the first item in the loop however, for all the sql rows that follow, clicking the image doesn't open up the modal-dialog box.
I don't have any overlapping tags, I've checked thoroughly. I've moved around the js script tags and don't get any change either. Is there a common cause for this? where would be the first place to troubleshoot? Would using a different type of loop in PHP be preferable?
<?php
ob_start();
session_start();
require_once ('verify.php');
?>
<head>
<title>Edit Listings</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<link href="../css/cropper.min.css" rel="stylesheet">
<link href="../css/crop-avatar.css" rel="stylesheet">
</head>
<body>
<div id="container">
<div id="head">
<ul id="menu">
</ul>
</div>
<div id="area"></div>
<div id="main_listings">
<h1 align="left">Edit listings page</h1>
<?php
include ("../dbcon2.php");
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM listings ORDER BY date_added DESC";
$result = $conn->query($sql);
?>
<?php while ($data=mysqli_fetch_assoc($result)):
$id = $data['id'];
$id = $data['title'];
$listing_img = $data['listing_img'];
?>
<div id="edit_listing">
<div id="edit_left">
<div class="container" id="crop-avatar">
<div class="avatar-view" title="Change the avatar"> <img src="<?php echo $listing_img; ?>" alt="<?php echo $title; ?>"> </div>
<div class="modal fade" id="avatar-modal" tabindex="-1" role="dialog" aria-labelledby="avatar-modal-label" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form class="avatar-form" method="post" action="edit-avatar.php" enctype="multipart/form-data">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" id="avatar-modal-label">Listing Main Image</h4>
</div>
<div class="modal-body">
<div class="avatar-body">
<div class="avatar-upload">
<input class="avatar-src" name="avatar_src" type="hidden">
<input class="avatar-data" name="avatar_data" type="hidden">
<input name="avatar_id" type="hidden" value="<?php echo $id; ?>">
<label for="avatarInput">Local upload</label>
<input class="avatar-input" id="avatarInput" name="avatar_file" type="file">
</div>
<div class="row">
<div class="col-md-9">
<div class="avatar-wrapper"></div>
</div>
<div class="col-md-3">
<div class="avatar-preview preview-lg"></div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
<button class="btn btn-primary avatar-save" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
<div class="loading" tabindex="-1" role="img" aria-label="Loading"></div>
</div>
</div>
<div id="edit_right">
<form name="edit_date" action="edit_list.php" method="post" id="edit_list_data">
<input name="title" type="text" id="title" tabindex="1" value="<?php echo $title; ?>" size="60" maxlength="57"/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" formaction="edit_list.php" value="Submit" />
</form>
</div>
</div>
<?php endwhile;$conn->close();?>
<div class="spacer"></div>
</div>
</div>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="../js/cropper.min.js"></script>
<script src="../js/crop-avatar.js"></script>
</body>
</html><?php // Flush the buffered output.
ob_end_flush();
?>
js js
// Events
EVENT_MOUSE_DOWN = "mousedown touchstart",
EVENT_MOUSE_MOVE = "mousemove touchmove",
EVENT_MOUSE_UP = "mouseup mouseleave touchend touchleave touchcancel",
EVENT_WHEEL = "wheel mousewheel DOMMouseScroll",
EVENT_RESIZE = "resize" + CROPPER_NAMESPACE, // Bind to window with namespace
EVENT_DBLCLICK = "dblclick",
EVENT_BUILD = "build" + CROPPER_NAMESPACE,
EVENT_BUILT = "built" + CROPPER_NAMESPACE,
EVENT_DRAG_START = "dragstart" + CROPPER_NAMESPACE,
EVENT_DRAG_MOVE = "dragmove" + CROPPER_NAMESPACE,
EVENT_DRAG_END = "dragend" + CROPPER_NAMESPACE,
build: function () {
var $this = this.$element,
defaults = this.defaults,
buildEvent,
$cropper;
if (!this.ready) {
return;
}
if (this.built) {
this.unbuild();
}
$this.one(EVENT_BUILD, defaults.build); // Only trigger once
buildEvent = $.Event(EVENT_BUILD);
$this.trigger(buildEvent);
if (buildEvent.isDefaultPrevented()) {
return;
}
// Create cropper elements
this.$cropper = ($cropper = $(Cropper.TEMPLATE));
// Hide the original image
$this.addClass(CLASS_HIDDEN);
// Show and prepend the clone iamge to the cropper
this.$clone.removeClass(CLASS_INVISIBLE).prependTo($cropper);
// Save original image for rotation
if (!this.rotated) {
this.$original = this.$clone.clone();
// Append the image to document to avoid "NS_ERROR_NOT_AVAILABLE" error on Firefox when call the "drawImage" method.
this.$original.addClass(CLASS_INVISIBLE).prependTo(this.$cropper);
this.originalImage = $.extend({}, this.image);
}
this.$container = $this.parent();
this.$container.append($cropper);
this.$canvas = $cropper.find(".cropper-canvas");
this.$dragger = $cropper.find(".cropper-dragger");
this.$viewer = $cropper.find(".cropper-viewer");
defaults.autoCrop ? (this.cropped = TRUE) : this.$dragger.addClass(CLASS_HIDDEN);
defaults.dragCrop && this.setDragMode("crop");
defaults.modal && this.$canvas.addClass(CLASS_MODAL);
!defaults.dashed && this.$dragger.find(".cropper-dashed").addClass(CLASS_HIDDEN);
!defaults.movable && this.$dragger.find(".cropper-face").data(STRING_DIRECTIVE, "move");
!defaults.resizable && this.$dragger.find(".cropper-line, .cropper-point").addClass(CLASS_HIDDEN);
this.$scope = defaults.multiple ? this.$cropper : $document;
this.addListeners();
this.initPreview();
this.built = TRUE;
this.update();
$this.one(EVENT_BUILT, defaults.built); // Only trigger once
$this.trigger(EVENT_BUILT);
},
The root of your problem is two-fold (and there may be other issues behind this).
First, you have tons of duplicate id values in your HTML.
Here are some of the dups: edit_listing, container, edit_left, crop-avatar, avatar-modal, etc...
A given ID can only be used once in the entire HTML document. All of these id values need to be changed to class names (which can be used as many times as you want) and then any code or CSS that references them needs to be changed to refer to the class name, not the ID value.
This comes into play in your code when you do:
new CropAvatar($("#crop-avatar"));
Because this is an id reference, it will only select the first element in your page with that id. Thus, only the first listing is active. If you change the HTML to be:
<div class="container crop-avatar">
then, you can select all of them with a class selector .crop-avatar.
Second, your CropAvatar() constructor is only ever called once, but it's written as if it is only going to be operating on a single avatar. So, either you need to call CropAvatar() separately for each listing OR you need to rewrite CropAvatar() and it's event handlers to work for all the listings instead of just one.
You could probably make the existing CropAvatar() constructor work if you fix all the duplicate ID values and then change this:
var example = new CropAvatar($("#crop-avatar"));
to this:
$(".crop-avatar").each(function() {
new CropAvatar($(this));
});
This will call the CropAvatar() constructor for each listing.
These are the first two main issues I see. I cannot promise that there are not other things to fix also, but those issues can't be seen until these first two are fixed.

php cookies are not setting?

<?php
if (!isset($_COOKIE['firsttime']))
{
setcookie("firsttime", "no", time()+1500);
?>
<div class="row-fluid">
<div class="span4 center">
</div>
<div class="span4 center">
<div style="">
<div class="alert alert-info">
<button type="button" href="<?php setcookie("firsttime", "no", time()+1500); ?>" class="close" data- dismiss="alert">×</button>
<strong>Welkom</strong> <br />Dit is je eerste bezoek op wijktoernooi.nl ! <br /> Wil je je direct registreren? Klik hier!
</div>
</div>
</div>
<div class="span4 center">
</div>
</div>
<?php
}
else
{
// do or show nada !
}
?>
Based upon code above if no cookie is set it should set cookie firsstime and show the alert box
But it doesnt....when i reload the page i still see the alert box, where am i making a mistake?
You cannot send output before setting cookies. You need to understand HTTP.
So do this test: var_dump(headers_sent()); before you try to set a cookie.
If headers are sent, you're doing it wrong.
href="<?php setcookie(...); ?>" is by far the most creative use of PHP ever. PHP prints stuff. It does not react to HTML/DOM/JS events. It takes input and produces output. That's all it does.
I cannot give you any code that works here as you don't have the understanding required to make heads or tail of it. So figure out HTTP (client/browser & server/php communication), cookies and only then will you be able to fix this and do it right in the future.

Zend framework 1 submit form url / action

I'm working on a AJAX sign up form which will be submitted to the Zend Framework. Right now, the form's action doesn't seem to be executing. The response text is a duplicate of the current HTML document. Can someone please inform me of what I'm doing wrong, and what the correct URL should be? I've set the project up using modules. For example, should the correct url from the example below be: '/account/register/auth'
#application.ini
resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = account
resources.router.routes.register.defaults.controller = register
resources.router.routes.register.defaults.action = index
#application/modules/accout/controllers/RegisterController.php
<?php
class Account_RegisterController extends Zend_Controller_Action{
public function init(){
$this->view->register = new Account_Form_Register();
}
public function indexAction(){
}
public function authAction(){
$request = $this->getRequest();
$form = new Account_Form_Register();
if($request->isPost()){
print('submit');
}else{
print('nothing yet');
}
}
}
?>
#application/modules/account/views/scripts/register/index.phtml
<div class="register">
<form id="registerForm">
<h2 class="formHeader">Register</h2>
<div class="floatLeft">
<?php echo $this->register->fName; ?>
</div>
<div class="remaining">
<?php echo $this->register->lName; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->uEmail; ?>
</div>
<div class="remaining">
<?php echo $this->register->aEmail; ?>
</div>
<div class="">
<?php echo $this->register->phone; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->oPassword; ?>
</div>
<div class="remaining">
<?php echo $this->register->cPassword; ?>
</div>
<div id="formButton">
<?php echo $this->register->submit; ?>
</div>
</form>
</div>
As I said, maybe you should disable the view processing when doing AJAX action in order to return a json_encode($some_data) instead of HTML content.
Try
$this->_helper->viewRenderer->setNoRender(true);
Take a look at this article

Categories