Undefined index with session - php

I'm developing a chatbox script, and I have this page that checks if session is set, and if so, the certain elements of code should be hidden with jQuery. Here are my pages:
input.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#import "stil.css";
</style>
<title>Untitled Document</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="postme.js"></script>
<?php
include_once('check.php');
?>
</head>
<body>
<div id="wrap">
<div id="chat">
<div id="main">
</div>
<div id="input">
<form name="form"action="test.php" method="post">
<input type="text" name="tekst" id="msg" size="72" />
<input type="submit" name="dugme" value="posalji" id="dugme" />
</form>
</div>
</div>
</div>
<div id="black">
</div>
<div id="name">
<form name="yname">
<input type="text" name="tekst2" />
<input type="button" name="dugme2" value="Enter" onclick='send()' />
</form>
</div>
</body>
</html>
sesion.php:
<?php
session_start();
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
?>
check.php:
<?php
include('sesion.php');
if (!isset($sesion_n)){
echo "<script type='text/javascript'>$('#black').hide();$('#name').hide();</script>";
}
?>
postme.js:
function send(){
$.post('sesion.php',{ime:yname.tekst2.value},function(val){
if(val!=null) {
$('#black').fadeOut();
$('#name').hide();
alert(val);
}
}
)};
So the problem is that I get this error every time I run the page:
Notice: Undefined index: ime in C:\wamp\www\AJAX\sesion.php on line 3.
So can someone tell me what I'm doing wrong here?

if(isset($_POST['ime']))
{
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
}

it seems that $_POST['ime']; is undefined and that means that you are not posting it i guess.
Are you sure that yname.tekst2.value is the correct way to access the value of the field?
If you have firebug you can check in the "console" tab what parametrs have been posted.

It appears you're loading check.php manually. That'd be a GET request, and will trash your stored value, as _POST won't be set on those pages. Probably won't be the cause of the undefined index problem, but something to consider.
Check that the session's ID value stays constant between requests. If it's different each time, you're getting a brand new blank session on each request.

Related

Getting $_GET variables in other files

I want to get another variable in another file, but I am wondering if this will work since I am trying to get a $_GET variable. In file one (login_check_update.php):
$username = $_GET['username'];
And in file two:
else{
include 'login_check_update.php';
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $username; ?></b></p>
Would file two try and get the values in the URL on that page or will it get the previous URL in the previous page? As in will file's two $username variable be redundant and cancel each other out?
chat.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="chat.css" />
</head>
<?php
if(!isset($_SESSION['name'])){
loginForm();
}
else{
include 'login_check_update.php';
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $username; ?></b></p>
<p class="logout"><a id="exit" href="#">Exit Chat</a></p>
<div style="clear:both"></div>
</div>
<div id="chatbox"></div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
});
</script>
<?php
}
?>
</body>
</html>
Perhaps the following may help you clarify your question:
Two files, same directory.
First file, named one.php:
<?php
$name = $_GET['name'];
Second file named two.php:
<?php
include 'one.php';
echo $name;
If I call file two.php with the following query param: two.php?name=who
This will output:
who
As $name is in the same scope.
Think of the include crudely as it inserting a text snippet of the first file in-place.

How to carry data from a form on one file to another using PHP sessions?

I'm trying to make a form that acts as a search engine and returns results. However, the data from the user's entry is either unable to save to a session or the session cannot be passed to another file. Here is the code for the "home" search page and the "Search-Engine" results page.
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
<?php session_register(); session_start(); ?>
<?php $_GET['query'] = $_SESSION['Query']; ?>
</body>
</html>
Search-Engine.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
<?php session_start(); ?>
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I don't know the exact purpose of using Sessions in your form. But you are doing in a wrong way by starting Session in middle of page and using Sessions within the form. You can add value in Sessions in another page after submitting the form.
You can update your files in the below way:
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" name="query" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
</body>
</html>
Search-Engine.php
<?php session_start();
$_SESSION['Query'] = $_GET['query']; ?>
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>

Using PHP to add data to a SQL Table and how to show errors

Updated Problem:
I'm trying to use PHP to add information into a SQL table if there is no errors, but I am not sure how to get it to fully working so that it'll show the errors and such. Where do I put the PHP code? Also where what language would I use to display the errors?
Updated HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TheQuantumBros</title>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="MainStyles.css" />
<link rel="shortcut icon" href="favicon.ico" />
<style type="text/css">
.style2 {
font-size: xx-large;
color: white;
}
.style3 {
font-size: xx-large;
color: #008000;
}
</style>
</head>
<body style=";background-image:url('bg_wallpaper_black.jpg');margin:0;padding-top:0;">
<div id="topBar" class="topBar">
<h1 id="title" class="title">
<img src="TheQuantumBros3.png" alt="" style="padding-right:5px" />
TheQuantumBros
<img src="TheQuantumBros3.png" alt="" style="padding-left:5px"/>
</h1>
</div>
<div id="topDiv" class="topDiv" style="height:200px">
<div id="topDivFrame" class="topDivFrame" style="background-image:url('TheQuantumBros2.png');height:200px"></div>
</div>
<div id="menuBar" class="menuBar">
<button name="homeButton" class="menuButton1" onclick="javascript:window.location.href='http://www.tqbtest.comlu.com/'">Home</button>
<button name="forumsButton" class="menuButton2">Forums</button>
<button name="bfButton" class="menuButton2">BF P4F</button>
<button name="mcButton" class="menuButton2">Minecraft</button>
<button name="applyButton" class="menuButton2">Applications</button>
<button name="infoButton" class="menuButton2">About Us</button>
</div>
<div style="width:100%;height:150px;margin-top: 5px;margin-left:auto;margin-right:auto;text-align:center">
<div style="width:75%;height:100%;text-align:center">
<span style="width:75%;height:10%;margin-right:15%;margin-left:45%;text-align:center" class="style2">TheQuantumBros Registration</span>
<form method="post" action="register.php" style="height:100%;width:100%;text-align:center">
<span class="registrationText">First Name:</span>
<input name="firstname" type="text" class="registrationInput"/>
<span class="registrationText">Last Name:</span>
<input name="lastname" type="text" class="registrationInput"/>
<span class="registrationText">Username:</span>
<input name="username" type="text" class="registrationInput"/>
<span class="registrationText">Email</span>
<input name="email" type="text" class="registrationInput"/>
<span class="registrationText">Password</span>
<input name="password" type="password" class="registrationInput"/>
<span class="registrationText">Retype Password:</span>
<input name="password2" type="password" class="registrationInput"/>
<span class="registrationText">Region</span>
<select name="region" class="registrationInput">
<option>North America</option>
<option>South America</option>
<option>Europe</option>
<option>Asia</option>
<option>Africa</option>
<option>Australia</option>
</select>
<span style="width:25%"></span>
<input name="Submit1" type="submit" value="Submit" class="registrationSubmit"/>
<span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
</form>
</div>
</div>
Updated PHP:
$sql="SELECT username, email FROM Profiles";
$result=mysqli_query($con,$sql);
if($result === false){
echo mysqli_error($con);
}else{
while($row = mysqli_fetch_array($result)){
$rows[] = $row;
foreach ($rows as $row) {
if($row['username'] == $_POST['username'] || $row['email'] == $_POST['email']){
//Error: "Username or Email already in use"
}else {
if($_POST['password'] == $_POST['password2']){
if (strpos($_POST['email'],'#') !== false) {
//INSERT INTO code?
}else{
//Error: "Please enter a valid email."
}
}else{
//Error: "Passwords do not match."
}
}
}
mysqli_close($con);
}
}
}else{
//Error code: "Please fill in all fields"
}
?>
Your code is structured very strangely, and I suspect you're confused about what you should use to do what. Why are you sticking your PHP at the bottom of the page, after all the HTML? That, and the way you talk about your PHP code "getting triggered" in the comments makes me wonder if you understand what exactly PHP does. PHP is a server-side tool for building HTML dynamically. By the time your user sees the page, all your PHP has finished running and can't do anything more until they load the next page. I don't know exactly what you're trying to do here, but putting PHP after all your HTML is never the right solution.
If you're putting data into a form, and then reloading the same page with the form data, what you should do is put the bulk of your PHP at the top of the page, and then put little bits of PHP interspersed throughout your HTML wherever you want something dynamic. Javascript shouldn't be needed unless you're doing something fancy.
You are closing the html tag. So the page is ignoring the stuff afterward. Move the </body> and </html> tags to after your script. Also you don't need to echo the the scripts. Unless you are doing some php logic in your script that you are not showing.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
//All my HTML code...
//Related HTML code
<div>
<span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
</div>
<?php
//All my PHP code...
//Related PHP/JQuery code
echo '<script type="text/javascript">'
, '$("#notFilled").css("visibility","visible")'
, '</script>';
?>
</body>
</html>
your <script> is outside the <html> tag.. it should be placed before the closing </body> tag
Anyway, this is a really dirty code.. Mixin Html, JS and PHP?.. you need to completly separate them. It will save you some headaches
Hope this helps
Instead if trying to edit the css, it may be a good idea to use the .hide() and .show() methods. You can even use transitions to make it look nicer (.fadeIn()...)
In any case, I wouldn't recommend the involving php to load javascript, but at the end of the day, I'm not sure of the context of the script.
jQuery might not be loaded yet when you are calling the jQuery.css() function.
Make sure that the jQuery js file is being loaded before (make sure it appears above) any jQuery function calls.
first of all ..
1) load the jquery.js script in your head....
2) i don,t understand why are you using when you can use <script></script> and ways... put the <body> and
3)use document.ready function
updated code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
//load jquery script here
</head>
<body>
//All my HTML code...
//Related HTML code
<div>
<span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
</div>
<?php
//all your php code
?>
<script type="text/javascript">
$(function(){
$('#buttonID').click(function(){ //<--updated here
$("#notFilled").css("visibility","visible");
});
});
</script>;
</body>

Jquery, PHP Calling other form after click

Im just learning javascript the last days (started PHP some months ago).
So, my code make this:
e.g
http://controljuridico.com/video01/
I have three files.
An Html file with the form.
A javascript file with the functions after click on "enviar" (send) button.
A php that process the data.
HTML file (index.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link href="css/css.css" type="text/css" rel="stylesheet" media="screen" />
<script src="js/jquery.js" type="text/javascript" language="JavaScript"></script>
<script src="js/js.js" type="text/javascript" language="JavaScript"></script>
</head>
<body>
<div id="content">
<h1>Test.</h1>
<h1>Step 1) choose city</h1>
<br />
<br />
<label>test(*)</label>
<hr />
<br />
<form name="form_data" id="form_data">
<label>(*) City</label>
<br />
Medellin
<input type="radio" name="ciudad" value="Medellin" />
Manizales
<input type="radio" name="ciudad" value="Manizales"/>
Cali
<input type="radio" name="ciudad" value="Cali"/>
<br />
<label>(*) Especialidad</label>
<br />
<input type="button" value="Enviar" id="btn_enviar" />
<br />
<br />
<label id="mensaje"></label>
</form>
<div id="resultado" ></div>
</div>
</body>
</html>
js.js File
$(document).ready(function(){
$('#btn_enviar').click(function(){
if( validaRadio( 'ciudad','Ciudad' ) == false) return false;
$.ajax({
type:'POST',
url :'upload.php',
data: $('#form_data').serialize(),
beforeSend : function(){
$('#mensaje').html('Enviando datos...');
},
success: function (data){
$('#mensaje').html('Datos enviados correctamente.');
$('#resultado').html(data);
},
complete: function(){
$('#form_data').slideUp();
$('#resultado').slideDown();
}
});
});
});
Php File (upload.php)
<?php
$sergu = $_POST['ciudad'];
if ($_POST['ciudad'] == "Medellin") {
?>
<label>Ciudad Ingresada:</label>
<br />
<label><?php echo $_POST['ciudad'] ?></label>
<hr />
<?php
}else{
echo "it is not medellin....";
}
?>
So, this works very well but. What if I want this:
After click on "enviar" button also show another similar form at the left of this form.
I mean its just like choosing steps if you choose the step one I need another step and so on and I want that this another step just appear to the left of the previus form.
It is possible? how?
Thanks in advance for your help! I really appreciate it.
You could for example just hide the 'second form' when the page first loads via css like this:
<form id="second_form" style="display: none">...
and when your success function fires you remove the css like so:
success: function(){
...
$('#second_form').show();
}
Short answer: yes.
In jquery, there is .insertBefore(). An easy way to implement what you (kind of) want, is to just echo out the new form in the php, and instead of
$('#resultado').html(data);
Do the following:
$(data).insertBefore('#resultado');
This will insert the new form, echoed out by the PHP, under the previous one. Ofcourse you also have to delete the
$('#form_data').slideUp();
Else, the forms will be hidden.

<!DOCTYPE HTML> declaration causes div contents to NOT display

I have an html page which contains a div that displays the html from an external php file.
It works great until I add a DOCTYPE declaration to the html page. The page continues to function, except the external content does not appear in the div.
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
<head>
<title>Test Page</title>
<!--meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="./inc/jquery.js"></script>
<script type="text/javascript">
function getinfo()
{
$.post('prodinfo.php', { prodcode: prodcodeform.prodcodevar.value},
function(output)
{
$('#prodinfo').html(output).show;
});
}
function hideinfo()
{
$('#prodload').hide();
$('#openprodinfo').show();
}
function showinfo()
{
$('#prodload').show();
$('#openprodinfo').hide();
}
</script>
</head>
<body>
<input style="position:relative;" type="button" class="button" id="openprodinfo" title="Open" value="INFO" onclick="showinfo();">
<DIV id="prodload" style="position:absolute;margin-left:auto;margin-right:auto;display:none;text-align:center;background-color:#000000;z-index:200;border:1px solid #4e443b;">
<div id="prodinfo" style="position:relative;display:block;top:0;width:1000px;height:820px;background-color:#ffffff;margin-left:auto;margin-right:auto;">
</div>
<form name="prodcodeform">
<input type="text" readonly="readonly" id="prodcodevar" name="prodcodevar" value="nil" >
</form>
<div ID="prodinfobutton" style="position:relative;">
<input style="position:relative;" type="button" class="button" id="closeprodinfo" title="Close" value="CLOSE" onclick="document.getElementById('prodcodevar').value='nil'; hideinfo(); ">
</div>
<input type="button" id="button001" value="ONE" onclick="document.getElementById('prodcodevar').value='item1'; getinfo();">
<input type="button" id="button002" value="TWO" onclick="document.getElementById('prodcodevar').value='item2'; getinfo();">
</DIV>
</body>
</html>
You are switching to Standards mode, so your browser is no longer playing the game of being compatible with Internet Explorer 4.
prodcodeform.prodcodevar.value will error because prodcodeform is undefined.
You don't get a global variable for every element with an id or name in a document.
Change:
<form name="prodcodeform">
To
<form id="prodcodeform" method="post" action="prodinfo.php">
… and make it do something sane when a non-Ajax request gets posted (move it so it is around the buttons, make them submit buttons, and cancel the default event if the JS succeeds).
Then add:
var prodcodeform = document.getElementById('prodcodeform');
before you try to use the variable.
You started your body with </body> instead of <body>.

Categories