I have searched a lot but cannot find a solution yet. Hope anyone can help me with it, I will really appreciate.
I use a php news page with session. PHP script displays 10 news and if there is more news, visitor suppose to click "Next" button (I try to make it work with tag, but it doesn't work with usual submit button either). Page refresh using same script adding a value to page. Everything seemed to work except session. $_SESSION['page'] does not add value. I always come back to Page1.
<?php session_start();?>
<?php
if(isset($_SESSION['page']) && isset($_POST['next'])){
$_SESSION['page']++;
}
if(isset($_SESSION['page']) && isset($_POST['previous'])){
$_SESSION['page']--;
}
else{
$_SESSION['page']=1;
}
....//code reading file with news, counting values etc. that work good
//I tried several options:
//Option:1
if ($l == "en") {
echo '<p class="sider_link">Page: '.$_SESSION['page'].'. Unread news: '.$news_left;
echo '<form action="../en/news.php" method="post">';
echo '<a class="sider_link" href="#" name="next" onclick="this.form.submit()">Next Page</a></form></p>';
}
//Option:2
if ($l == "en") {
echo '<p class="sider_link">Page: '.$_SESSION['page'].'. Unread news: '.$news_left;
echo '<form action="../en/news.php" method="post">'
echo '<input type="hidden" name="next" value="yes" />';
echo '<a class="sider_link" href="#" onclick="this.form.submit()">Next Page</a></form></p>';
}
Your first statement works, page is increment. But after the second statement is false, the else condition is called. Page is set to one. To sum up you have an error in your condition.
if(isset($_SESSION['page']) && isset($_POST['next'])){
$_SESSION['page']++;
}
if(isset($_SESSION['page']) && isset($_POST['previous'])){
$_SESSION['page']--;
}
else{
$_SESSION['page']=1;
}
To
if(isset($_SESSION['page']) && isset($_POST['next'])){
$_SESSION['page']++;
}
else if(isset($_SESSION['page']) && isset($_POST['previous'])){
$_SESSION['page']--;
}
else{
$_SESSION['page']=1;
}
Related
Ok so I have a form with 1 input and a submit button. Now I am using an if/else statement to make three acceptable answers for that input. Yes, No, or anything else. This if/else is working the thing is the code is kicking out the else function as soon as the page is loaded. I would like there to be nothing there until the user inputs then it would show one of three answers.
Welcome to your Adventure! You awake to the sound of rats scurrying around your dank, dark cell. It takes a minute for your eyes to adjust to your surroundings. In the corner of the room you see what looks like a rusty key.
<br/>
Do you want to pick up the key?<br/>
<?php
//These are the project's variables.
$text2 = 'You take the key and the crumby loaf of bread.<br/>';
$text3 = 'You decide to waste away in misery!<br/>';
$text4 = 'I didnt understand your answer. Please try again.<br/>';
$a = 'yes';
$b = 'no';
// If / Else operators.
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
}
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
?>
<form action="phpgametest.php" method="post">
<input type="text" name="name" /><br>
<input type="submit" name="senddata" /><br>
</form>
You just need to call the code only when the POST value is set. This way it will only execute the code when the form was submitted (aka $_POST['senddata'] is set):
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
}
Just put the validation in the first if statement like this:
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a) {
echo ($text2);
} elseif ($usertypes == $b) {
echo ($text3);
} else {
echo ($text4);
}
}
When you load your page the browser is making a GET request, when you submit your form the browser is making a POST request. You can check what request is made using:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Your form was submitted
}
Put this around your form processing code in order to keep it from being executed on GET request.
if(count($fetchExperiences) > 0)
{
$i=0;
foreach($fetchExperiences as $fetchExperience)
{
$sqlExperiencedat = $db->createCommand(" code ");
$fetchExperiencesdat = $sqlExperiencedat->queryAll();
$tdyyy=date('YYYY-mm-dd');
if(strtotime($tdyyy)<strtotime($fetchExperience['experience_date']))
{
foreach($fetchExperiencesdat as $fetchExperiencesdatt)
{
if(strtotime($tdyyy)<strtotime($fetchExperiencesdatt['avail_date']))
{
//echo $fetchExperiencesdatt['avail_date'];
$tth=1;
}
}
if($tth==1){
//fetchExperiencesdatt code
}
}
}
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
}
}
My question is that I want more button after $fetchexperiences count is greater than 8. It is fine. But in between $fetchexperiences loop there is a loop for $fetchExperiencesdat. by the loop if there is no experince with in availdate it is not shown. so the probelm is more button is display not after 8 because of some $fetchExperiencesdat elements should be hidden. So please give me any solution for this.
Thanking you.
I'm not sure I understand where your count experiences are coming from. However, the problem your code has is that fetchCountExperiences is not given a value.
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
Looks like it should be
if(count($fetchExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
We have a form that Span Across multiple pages and the form is full of checkboxes and radio-buttons. Requirement is that when a user navigates across multiple pages of the form, the user should be able to see the checkboxes and radio-buttons he has already Selected before the form Submit button is pressed.
I am copying the code segement that is used to generate the Checkboxes in the form - this is a Sample code Only.
<form action="car_model.php" method="post" name="car_form" id="car_form">
$q10 = "SELECT ...
$r10 = mysqli_query ($dbc, $q10);
if (mysqli_num_rows($r10) > 0) {
while ($row10 = mysqli_fetch_array($r10, MYSQLI_ASSOC))
{
echo '<p class="normal_text"><input type="checkbox" name="model_selection[]" value="' . $row10['model_id'] . '" onclick="return KeepCount()"; />' . $row10['car_model_name'] . '</p></br>';
}
}
</form>
if ($pages > 1) {
echo '<br /><p>';
// Determine what page the script is on:
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo 'Previous ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo $i . ' ';
}
}
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo 'Next';
}
echo '</p>';
}
I understand that JQuery is a Good tool for this purpose - how to use it?
If using server side session variables is not an option then you could implement it in such a way that when you click "next" instead of loading a new page you just hide the current content place holder of the visible part of the form and show the next one at it's place and the same when clicking back.
This can easily be done with jQuery. Here is a very simple example to get you started:
$("#next").click( function () {
$("#page1").hide();
$("#page2").show();
});
$("#back").click( function () {
$("#page1").show();
$("#page2").hide();
});
Fiddle
Good luck!
I have the following php code:-
<?php
.....
......
if(isset($_POST['disable']))
{
foreach ( $_POST['users'] as $userid ) {
$wpdb->query("UPDATE ".$wpdb->prefix."usercontrol SET disable_status ='disabled'
WHERE ID = ".$wpdb->escape($userid));
}
}
?>
HTMl
-----
<input type="submit" value="<?php esc_attr_e('Disable'); ?>" name="disable"
id="disable" class="button-secondary action" />
This is the page:-
I need to simply refresh /reload the page, if the user didn't check in any of the check box.
now its throwing error. Need to write an else condition for "if(isset($_POST['disable']))"
Please help.
<?php
.....
......
if(isset($_POST['disable']))
{
if(isset($_POST['users'])) {
foreach ( $_POST['users'] as $userid ) {
$wpdb->query("UPDATE ".$wpdb->prefix."usercontrol SET disable_status
='disabled' WHERE ID = ".$wpdb->escape($userid));
}
}
}
?>
Form submit does reload the page, since in your case it's not bound to an AJAX call. Just added an isset that checks for any value for users in $_POST. Optionally, you can even add an else to display an informative message instead of a plain reload.
I need a simple php function to mimic an Ajax form submission - basically in the form there is a radio button "ajax" and it is either set to yes or no. I just need to mimic successful/failing ajax calls...
HTML
<label for="">Ajax Success*<input type="radio" name="ajax" id="yes" value="yes" checked>Yes<input type="radio" name="ajax" id="no" value="no">No</label>
PHP
<?php
$ajax = $_POST["ajax"];
if(isset($_POST['ajax'] == "yes")) {
echo "success";
} else {
echo "failure";
}
?>
if I remove the isset, I get an 'undefined index' error, if I put it in I get a syntax error but it looks correct to me...
I just need to send back an echo depending on what option is selected for the input 'ajax'
thx
isset($_POST['ajax'] == "yes") doesn't make sense. You want to check if it's set, and then check if its value is equal to "yes":
if(isset($_POST['ajax']) && $_POST['ajax'] == "yes") {
echo "success";
} else {
echo "failure";
}
As your code does, I'll say you use your defined variable like in this example:
<?php
$ajax = $_POST["ajax"];
if($ajax == "yes") {
echo "success";
} else {
echo "failure";
}
?>
Because if the variable has the value "yes" it'll be ok and undefined or other value will end in "failure".