can't i display two session message in one page - php

i have been displaying two session messages in one page, rating. One message is displayed in every refresh showing the likes and dislikes. the other message in displayed once the rating form is submitted, the problem is once i submit the rating form the message displaying the rating isn't displayed. why is so??? can't i use two sessions in a page?? thanks
coding
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>

sorry i think my code is a bit confusing
my actual code looks like this
<div class="top_model">
<h1>Model Ratings</h1>
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
<ul class="homemodel_thumbs">
<div style="text-align: right">
</div>
<?php
if(!empty($ratingArr)){
foreach($ratingArr as $ratings){
?>
<li><img src="<?php echo base_url();?>images/gallery/large/<?php echo $ratings['gallery_image'];?>" alt="<?php echo $ratings['gallery_image'];?>" class="center_image"/>
<p> </p>
<?php
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
//session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
</li>
<?php
}
}
?>
</ul>

this problem is solved. of the two session message for the one i used php session while for the another one i used code igniter session. sorry if i haven't mentioned i am using ci. anyways thanks everyone for the help :)

You're unsetting your rating message after you display it:
session_unset($_SESSION['rate_msg']);
This means it's not available after you've posted back the form unless you re-populate it again.
Remove this and i suspect it'll work.

You have an excess ?> :
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
if(!empty($_SESSION['rate_msg']))
{
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>

You don't open php tag for last statement, (or unnesesery close it) it's treated as html
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?php
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
EDIT
<?php
// < HERE IS PHP >
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<!-- HERE IS HTML -->
<?
// < HERE IS PHP >
session_unset($_SESSION['error_msg']);
}
?> <!-- CLOSED PHP - NOW IT'S HTML - YOU SHOULD REMOVE IT -->
<!-- THIS IS NOT EXECUTED - BECAUSE IT'S HTML NOW -->
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?> <!-- THIS IS NOT PHP TAG BECASUE THERE IS NOT OPEN TAG -->
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>

Related

$_POST not sending all values of radio buttons

I'm making an online quiz system using PHP, I've saved questions, their options and answers in a table in database. In the html page, I'm printing the questions and options through a while loop (options are radio buttons). I learned how to send radio button values to PHP, but the problem I'm facing is that, only the selected option for the first question is sent to PHP. Like example, if I've saved two questions in database and they are printed on the page using a while loop, now when if I select option1 for question1 and option2 for question2, and click on submit, this just posts the value option1 to PHP, and the others selected button values are lost. I want all these value to check through database if the answers are correct and then display score based on correct answers.
Here is the code of my html page, I'm posting the whole code, but I think you should just check the <form> tag.
<body>
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>
<div id="container">
<div class="header">
<span class="heading"><span class="C">O</span>NLINE <span
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
<div class="logo"><img src="img/flogo.png" width="188" height="150"
alt="logo"></div>
</div>
<div class="center">
<nav>
<ul>
<li style="margin-right:180px; height:50px;width:auto">Username</li>
<li>Home</li>
<li>Results</li>
<li>Sign Out</li>
</ul>
</nav>
<div id="page-wrap">
<h1>HTML Quiz </h1>
<form action="results.php" method="post" id="quiz">
<ol>
<li>
<?php
while($result=mysql_fetch_assoc($record))
{
echo "<form>";
echo "<h3>";
echo "".$result['id'].". ".$result['question']."";
echo "</h3>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
echo '<label for="question-1-answer-A">';
echo "".$result['option1']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
echo '<label for="question-1-answer-B">';
echo "".$result['option2']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
echo '<label for="question-1-answer-C">';
echo "".$result['option3']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
echo '<label for="question-1-answer-D">';
echo "".$result['option4']."";
echo "</label>";
echo "</form>";
}
echo '<input type="Submit" value="Submit" name="submit" />';
?>
</li>
</ol>
</form>
</div>
</div>
<div class="footer">
<footer></footer> </div>
</div>
</body>
And the results.php
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
$result=mysql_fetch_assoc($record);
$total=0;
foreach($_POST['answer'] as $check) {
echo $check;
}
echo "YOUR SCORE: ".$total;
?>
I know this results file does nothing right now, the foreach part will be used to check every answer with the answer in database and then total will be incremented, the code you see right now is just to check how many values are being sent to PHP. Currently I've printed 6 questions, and after selecting options for every question, when I click submit, it displays
AYOURSCORE: 0
Here 'A' being the value of the option selected in "Question 1", values for other questions are lost. Which is the main issue I'm facing.
I've to submit this project in 2 days, so any help will be greatly appreciated.
Thanks
P.S. I am new to PHP` and have very little or no knowledge of java Script.
I think you are using two form. Where inside one form you repeating multiple for but in child form you have not provide action="". Or remove form from inside the loop.
<body>
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>
<div id="container">
<div class="header">
<span class="heading"><span class="C">O</span>NLINE <span
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
<div class="logo"><img src="img/flogo.png" width="188" height="150"
alt="logo"></div>
</div>
<div class="center">
<nav>
<ul>
<li style="margin-right:180px; height:50px;width:auto">Username</li>
<li>Home</li>
<li>Results</li>
<li>Sign Out</li>
</ul>
</nav>
<div id="page-wrap">
<h1>HTML Quiz </h1>
<form action="results.php" method="post" id="quiz">
<ol>
<li>
<?php
while($result=mysql_fetch_assoc($record))
{
echo "<h3>";
echo "".$result['id'].". ".$result['question']."";
echo "</h3>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
echo '<label for="question-1-answer-A">';
echo "".$result['option1']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
echo '<label for="question-1-answer-B">';
echo "".$result['option2']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
echo '<label for="question-1-answer-C">';
echo "".$result['option3']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
echo '<label for="question-1-answer-D">';
echo "".$result['option4']."";
echo "</label>";
}
echo '<input type="Submit" value="Submit" name="submit" />';
?>
</li>
</ol>
</form>
</div>
</div>
<div class="footer">
<footer></footer> </div>
</div>
</body>
I think, that the problem is in name of the input: name="answer[]". It is the same name for every radiobutton - the same for all questions. I think, that adding an index into brackets will help
name="answer[1]" for question one
name="answer[2]" for question two
and so on.
The w3schools TryIt Editor lets you play with inputs.

Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. I have searched through the forum & did not find an answer that suits my need. I have 2 files: jobs.php & jobprocess.php
Jobs.php goes as
<?php session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
echo "Opportunity is". $opportunity;
?>
<html>
<head>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</head>
<body>
</body>
</html>
jobprocess.php goes with the code
<?php session_start();
include('dbConnect.php');
$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>
Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. Thanks in advance
If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc
<?php
session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
?>
<html>
<head>
<title>must have a title</title>
</head>
<body>
<?php
echo "Opportunity is". $opportunity;
?>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</body>
</html>
And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable
<?php
session_start();
include('dbConnect.php');
$opportunity = $_POST['opportunity'];
echo "opportunity is " . $opportunity;
?>
Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! I guess session variables are illegal now?
The answer was chosen for the best answer even.

How to insert the hidden field data into database using php

Hi While login into the site iam getting the username in hidden field while filling the form after login i need to insert the username in database.But iam getting The error as
Notice: Undefined index: email
Here is my code
index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>
details.php
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$name=$_POST['user_name'];
$query=mysql_query("INSERT INTO user_details(email,user_name)values('$email','$name')");
Can anyone help me this thanks in advance
While filling this form i need to insert the hidden field value also in database
You hidden field must be inside your form . Otherwise its value not submit form action and you will get Undefined Notice
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
--- rest code---
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
</form>
You are getting Undefined Index notice because you are not using your hidden input inside the <form></form>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
Keep in mind either filed hidden or visible you will define it inside the <form></form> otherwise you will get the Notices.
Side Note:
Please use mysqli_* or PDO extension, because mysql_* is deprecated and not available in PHP 7.
One more issue, your submit button also out side the <form> but it will work because you are using onclick() event, but its not good practice.
Your code is open for SQL Injection, you need to prevent with SQL Injection.
One last thing, i hope you are using session_start() on top in your file for getting $_SESSION['username']; in your original code, please recheck.
Try putting that field in form tag and you done.
<?php
$username = $_SESSION['username'];
if($username){?>
<h3> Welcome <?php echo $username; ?></h3>
<?php } else {
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th>
</tr>
<tr>
<td>
<input type="text" name="user_name" value="" required />
</td>
</tr>
index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>
<?php
session_start();
// you have to start the session in order to work with php session.
$username = $_SESSION['username'];
?>
> <form method="post" action="details.php" id="myform"> <input
> type='hidden' value="<?php echo $username; ?>" name='email'>
>
> <input type="button" value="Logout" id="logout"
> onClick="document.location.href='login.php'" />
>
> </form>
hidden type always contain hidden value

add search ajax ability to this html form

I am stuck trying to add some ajax search ability to this php form. The form is already working but since I have many outputs, I want users to be able to search and select a profile from there. I tried jquery tablesorter and although it works, it's not quite what I was looking for.
Hope someone can help. Thanks. Here is the form.
<form action="<?php echo CRoute::getURI(); ?>" method="post" id="jomsForm" name="jomsForm" class="community-form-validate">
<div class="jsProfileType">
<ul class="unstyled">
<?php
foreach($profileTypes as $profile)
{
?>
<li class="space-12">
<label for="profile-<?php echo $profile->id;?>" class="radio">
<input id="profile-<?php echo $profile->id;?>" type="radio" value="<?php echo $profile->id;?>" name="profileType" <?php echo $default == $profile->id ? ' disabled CHECKED' :'';?> />
<strong class="bold"><?php echo $profile->name;?></strong>
</label>
<?php if( $profile->approvals ){?>
<span class="help-block"><?php echo JText::_('COM_COMMUNITY_REQUIRE_APPROVAL');?></span>
<?php } ?>
<span class="help-block">
<?php
$profile->description = JHTML::_('content.prepare',$profile->description);
echo $profile->description;?>
</span>
<?php if( $default == $profile->id ){?>
<em><?php echo JText::_('COM_COMMUNITY_ALREADY_USING_THIS_PROFILE_TYPE');?></em>
<?php } ?>
</li>
<?php
}
?>
</ul>
</div>
<?php if( (count($profileTypes) == 1 && $profileTypes[0]->id != $default) || count($profileTypes) > 1 ){?>
<div style="margin-top: 5px;">
<?php if( $showNotice ){ ?>
<span style="color: red;font-weight:700;"><?php echo JText::_('COM_COMMUNITY_NOTE');?>:</span>
<span><?php echo $message;?></span>
<?php } ?>
</div>
<table class="ccontentTable paramlist" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td class="paramlist_key" style="text-align:left">
<div id="cwin-wait" style="display:none;"></div>
<input class="btn btn-primary validateSubmit" type="submit" id="btnSubmit" value="<?php echo JText::_('COM_COMMUNITY_NEXT'); ?>" name="submit">
</td>
<td class="paramlist_value">
</td>
</tr>
</tbody>
</table>
<?php } ?>
<input type="hidden" name="id" value="0" />
<input type="hidden" name="gid" value="0" />
<input type="hidden" id="authenticate" name="authenticate" value="0" />
<input type="hidden" id="authkey" name="authkey" value="" />
</form>
I would suggest you Select2. It's a really well done jquery-based library with easy-to-use ajax and custom-rendering features. You can check the Loading Remote Data section of the documentation.
It can be configured to fetch data from the server, after N chars have been inserted by the user. Then you simply have to implement the server-side search action and with very few code you've a working (and very good looking) ajax selector.

JOMSOCIAL INVALID TOKEN ERROR

Evry time while logging in an error "INVALID TOKEN" its shown at the joomla2.5/jomsocial2.4 site, but after 2,3 refreshes it shows the profile page as logged in... why is it so.... Is it problem of Joomla... I have uninstalled a plugin alphauserpoints ... is that a problem.. or should i modify the code default.php
<?php
// no direct access
defined('_JEXEC') or die;
JHtml::_('behavior.keepalive');
?>
<?php if ($type == 'logout') : ?>
<form action="<?php echo JRoute::_('index.php', true, $params->get('usesecure')); ?>" method="post" id="login-form">
<?php if ($params->get('greeting')) : ?>
<div class="login-greeting">
<?php if($params->get('name') == 0) : {
echo JText::sprintf('MOD_LOGIN_HINAME', $user->get('name'));
} else : {
echo JText::sprintf('MOD_LOGIN_HINAME', $user->get('username'));
} endif; ?>
</div>
<?php endif; ?>
<div class="logout-button">
<input type="submit" name="Submit" class="button" value="<?php echo JText::_('JLOGOUT'); ?>" />
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.logout" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
<?php else : ?>
<form action="<?php echo JRoute::_('index.php', true, $params->get('usesecure')); ?>" method="post" id="login-form" >
<?php if ($params->get('pretext')): ?>
<div class="pretext">
<p><?php echo $params->get('pretext'); ?></p>
</div>
<?php endif; ?>
<fieldset class="userdata">
<p id="form-login-username">
<label for="modlgn-username"><?php echo JText::_('MOD_LOGIN_VALUE_USERNAME') ?></label>
<input id="modlgn-username" type="text" name="username" class="inputbox" size="18" />
</p>
<p id="form-login-password">
<label for="modlgn-passwd"><?php echo JText::_('JGLOBAL_PASSWORD') ?></label>
<input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18" />
</p>
<?php if (JPluginHelper::isEnabled('system', 'remember')) : ?>
<p id="form-login-remember">
<label for="modlgn-remember"><?php echo JText::_('MOD_LOGIN_REMEMBER_ME') ?></label>
<input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
</p>
<?php endif; ?>
<input type="submit" name="Submit" class="button" value="<?php echo JText::_('JLOGIN') ?>" />
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
<?php echo JHtml::_('form.token'); ?>
</fieldset>
<ul>
<li>
<a href="<?php echo JRoute::_('index.php?option=com_users&view=reset'); ?>">
<?php echo JText::_('MOD_LOGIN_FORGOT_YOUR_PASSWORD'); ?></a>
</li>
<li>
<a href="<?php echo JRoute::_('index.php?option=com_users&view=remind'); ?>">
<?php echo JText::_('MOD_LOGIN_FORGOT_YOUR_USERNAME'); ?></a>
</li>
<?php
$usersConfig = JComponentHelper::getParams('com_users');
if ($usersConfig->get('allowUserRegistration')) : ?>
<li>
<a href="<?php echo JRoute::_('index.php?option=com_users&view=registration'); ?>">
<?php echo JText::_('MOD_LOGIN_REGISTER'); ?></a>
</li>
<?php endif; ?>
</ul>
<?php if ($params->get('posttext')): ?>
<div class="posttext">
<p><?php echo $params->get('posttext'); ?></p>
</div>
<?php endif; ?>
or does the uninstallation affected the configuration file of joomla
Try this link: http://developersbench.in/invalid-token-during-registration-jomsocial/#.T19wtHm45F0 . You might get some help from this.
The info i have about this is every time you open a page and submit a form it first validates the token and if token is expired which is mainly because of remaining idle on the page for a few time, then you get "Invalid token" error.
Simply if you are not worried about the token, go to the file root/libraries/joomla/environment/request.php and check the below function
function checkToken( $method = 'post' ), comment all the lines between this and simply return true, whatever be the case. I haven't tried this, but i guess this will work.

Categories