Access session value - javascript

How to make a form style hidden if a specific session is set?
I did it that way
<form action="" method="post" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
<input type="radio" name="log" id="viso" value="vis" checked onclick="javascript:yesnoCheck();"> Visitor<br>
<input type="radio" name="log" id="uso" value="use" onclick="javascript:yesnoCheck();" > User<br>
<label name="frm" id="frm" style="display:none" ><b>Password</b></label>
<input type="password" style="display:none" name="pw" id="pw" placeholder="Enter Password" >
<input type="submit" >
</form>
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
unset($_SESSION["start"]);
}
else if ( isset ($_POST['log'])&& $_POST['log']=="use" )
{
if ( isset($_POST['pw']) && $_POST['pw']!="1111" )
echo "Wrong password";
else if ( isset ($_POST['pw'])&& $_POST['pw']=="1111" )
{
$_SESSION['start']="Welcome";
unset($_SESSION["visitor"]);
}
}
?>
but it's only hidden if i press on the submit button twice not from the first time

You have a simple logical flaw .You are checking for session before you set them .When user fills form and submits , your php runs this
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
Note that at this time no session is set so form will not have display:noneand will get displayed **even though user has entered his details correctly still when response come form will be visible.Now to fix the issue move your session check before form display code
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
//....more stuff
//...
} ?>
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>

Related

can we send the id to the file itself with submit form

I tried to post id to file itself in php. But it post nothing. This code I tried
//if can it should echo 1 and point 232
<?php
if ($_POST['submit'] == 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners" onclick="location.href='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML'">
</form>
You want to GET value inside span in PHP using JavaScript. Try something like this:
<?php
$a = isset( $_GET['A'] ) ? $_GET['A'] : '';
echo $a;
$point = isset( $_GET['POIN'] ) ? $_GET['POIN'] : '';
echo $point;
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input
class="process"
name="submit"
type="submit"
id="submit"
value="winners"
onclick="window.location='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML;return false;"
>
</form>
If you try to pass parameters like a query string you do not need to use a form.
Because you configure your form to send a post request, you can pass the parameters of the query string to input values of hidden type. Test as follows
<form action="reward-pollingx.php" enctype="multipart/form-data" method="post" style="width:auto;">
<input type="hidden" name="A" value="1">
<input type="hidden" name="POIN" value="">
<input type="hidden" name="submit" value="winners">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners">
</form>
<script>
document.querySelector('input[name=POIN]').value = document.getElementById('timespan').innerHTML;
</script>
Your php code should change to use $_POST instead of $_GET
<?php
if ($_POST['submit'] === 'winners') {
$a = $_POST['A'];
echo $a;
$point = $_POST['POIN'];
echo $point;
}
Or use query string to pass parameters to the PHP script
Submit
And the php
if ($_GET['submit'] === 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
To build the query string because you need to get information from the view you probably need to build it dynamically
If you need to submit the form to itself, you can use
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" ....
or you can use:
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST" ....

AJAX form with priority values

I want to create form with priority values, first check vlaue of recaptcha if is correct (not spammer) then do process for example:
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha)
{
echo "enter captcha";
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=*******&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==TRUE){
$name = $_POST['name'];
if($name == "hello"){
echo 'correct';
}
else {
echo 'incorrect';
}
}
if($response.success==false)
{
die("spammer!");
}
and HTML code like:
<form id="form" method="POST" action="">
<input id="name" name="name" type="text"/>
<div class="g-recaptcha-response" data-sitekey="******"></div>
<input id="button" type="submit" value="send" />
</form>
<div id="formresult"></div>
<div id="formresult"></div> is for ajax answer
I wrote some script code but not worked and I prefer ask without paste that code in here. how could I do that?

Page refresh after login?

I am trying to get my page to refresh after login this is the format of my index.php
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
// let the user access the main page
// i have made the main content with jquery mobile framework
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
// let the user login
include "login.php";
}
else
{
// display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
Inside my login.php
<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['email'];
$uid = $row['u_id'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['uid'] = $uid;
$flag=1;
header("Location: http://5aday.dihops.net/index.php");
// echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">";
//header("Refresh: 2; url=http://5aday.dihops.net/index.php");
// THIS IS WHERE I AM STUCK
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please click here to try again.</p>";
}
?>
}
?>
I dont know what I am doing wrong. But once the user gives his username and password and submits it .... I get a blank page. and the user has to manually refresh the webpage to see the content of the webpage.
As per the link mentioned the header(location) must be placed at the top of the page ... So I rearranged the if conditions ....
I added this at the top of the index.php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
include "login.php";
} ?>
then in the body i added the other if statements
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
// let the user access the main page
// i have made the main content with jquery mobile framework
}else
else
{
// display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
}
That has fixed the problem.
You have no content under:
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
// let the user access the main page
// i have made the main content with jquery mobile framework
}
So.. when you log in, there's nothing to see.
It doesn't seem there's any HTML elements for JavaScript to grab a handle on and edit either. Unless you're using JavaScript to then create elements from scratch. If so, have you checked that the JavaScript has even loaded? Also, check for errors in the console.
Try this code
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
echo "Welcome".$_SESSION['Username'];
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
// let the user login
include "login.php";
}
else
{
// display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
Try using PHP_SELF for redirect the same page
header('Location:'.$_SERVER['PHP_SELF']);
it might help
Stay page after Login, Refresh page. try JAVASCRIPT in php
echo "<script>window.location.replace('/')</script>";

SESSION variable slow to update textfield in php

I have a form, contained 3 fields, which when the submit button in submitted,
it will changes the session variable values according to the fields, in this case there are 3 variables. then i echo back the variable onto the fields.
For the first time submit, it stores the value beautifully and display in the fields correctly, the problem is that, when i submit the second time, the values are still the same. after i refresh the page, then the values in the fields are changed.
this is partially the codes i'm using now.
<?php
session_start();?>
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
}
?>
After i refresh(F5), then the value changed. i want it to be, when i clicked the submit button, it will change to the new value.
PHP Code:
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
echo '<script type="text/javascript">'
, 'jsfunctionToPrintUpdatedValues();'
, '</script>'
;
}
?>
Javascript Sample Code
function jsfunctionToPrintUpdatedValues()
{
/* retrieve the updated session variables in javascript variables */
var acc_main_js = <?php echo $_SESSION['acc_main']?>
var acc_id_js = <?php echo $_SESSION['acc_id']?>
var acc_cat_js = <?php echo $_SESSION['acc_cat']?>
document.getElementById("main").value=acc_main_js;
document.getElementById("id").value=acc_main_js;
document.getElementById("cat").value=acc_main_js;
}
in the input fields you have to write like this
`
Below
`
<input type="text" name="acc1" value="<?php if(isset($_SESSION['acc_main'])) echo $_SESSION['acc_main']" />
Use if(isset($_POST['submit']) != '') before the form. Change your code to this:
<?php
session_start();
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
}else{
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
<?php } ?>

codeigniter before submit confirm popup box using javascript

I need when I click the button for submit view popup box for confirmation and if I press ok, it will continue the submit and if cancel nothing will happen and close the box
here is my code,
code in the view form
<?php
foreach($company_data as $row){
?>
<h2>Company Details.</h2>
<?php echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" name="submit" value="Update" onclick="update(<?php echo $row->name;?>)"/>
</form>
<?php } ?>
You can put confirm() dialog in your update function like this:
function update(args){
if(confirm('Do you want to submit?')){
// put all the js code here.
}else{
return false;
}
}
So whenever you call your update function it will show a confirm box to ask user to submit the form if user clicks ok then it submits else form won't get submitted.
in onclick function call a javascript function and create confirm box using java script and check the return value from the confirm box if it is true then submit the form by using javascript onsubmit function
here is the answer
<form action="<?php echo site_url('site/update'); ?>" name="myForm" onsubmit="return(validate());">
<?php// echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" value="Update"/>
</form>
<?php } ?>
<script type="text/javascript">
function validate()
{
var r=confirm("Do you want to update this?")
if (r==true)
return true;
else
return false;
}
</script>
First, here's the classic jQuery way to do this.
$(document).ready(function(){
$( 'form#yourFormId' ).submit( function(){
if( confirm('Really submit this form?') ){
return TRUE;
} else {
return FALSE;
}
})
})
However, your code doesn't appear to submit the form directly, so this probably won't work with your code as written.
You haven't provided the code for the update() method specified in your onClick attribute, but you should be able to add confirmation code to the top of it like this:
function update(name){
if( !confirm('Really submit this form?') ){
return FALSE;
}
// ....the rest of your code...
}
you can use below one line
<?php
foreach($company_data as $row){
?>
<h2>Company Details.</h2>
<?php echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" name="submit" value="Update" onclick="return confirm(<?= $row->name ?>);"/>
</form>
<?php } ?>

Categories

Resources