I want to set OR in isset($_POST), so that there will be only one button to save in database.
btnusersave is the one that will save the whole data in dialog,
while saveuseraccess button is the one that saves the check boxes in database.
This is my code for btnusersave
<?php
$eventrequirements = 'unchecked';
if (isset($_POST['btnusersave']))
{
if (isset($_POST['eventrequirements']))
{
$eventrequirements = $_POST['eventrequirements'];
if ($eventrequirements == 'net') {
$eventrequirements = 'checked';
}
}
}
?>
Perhaps something like this?
<?php
$eventrequirements = 'unchecked';
if (isset($_POST['btnusersave'])) {
if (isset($_POST['eventrequirements'])) {
$eventrequirements = $_POST['eventrequirements'];
if ($eventrequirements == 'net') {
$eventrequirements = 'checked';
}
}
}else if ( isset($_POST['saveuseraccess']) ) {
//do alternate code (to save checkboxes) here
}
?>
Related
On a blog I'm coding the admin can edit existing posts.
I want to let an error-message appear when the $_POST['title'] for e.g is empty(There will be displayed:"Your post should have a title"). I also do it if the subheading, content or category are empty.
The errors work just fine if one or some of them is/are empty. As soon I load the page to edit a post every error is displayed from the beginning.
How do I make them only appear when one or some $_POST's are empty after the <input type="submit .../> is clicked (they shouldn't be there when the site has loaded)?
This is the function in the PostsAdminController.php that checks the $_POST's and renders the site:
public function edit()
{
$error = "";
$id = $_GET['id'];
$entry = $this->postsRepository->find($id);
$categoryFId = $this->categoryRepository->getOneCatFromId($entry->c_Id);
$savedSuccess = false;
$abort = false;
if ($this->loginService->check()) {
if (!empty($_POST['title'])) {
$entry->title = $_POST['title'];
} else {
$error .= "Your post should have a title.";
$abort = true;
}
if (!empty($_POST['subheading'])) {
$entry->subheading = $_POST['subheading'];
} else {
$error .= "A good subheading is nothing you should just leave out.";
$abort = true;
}
if (!empty($_POST['content'])) {
$entry->content = $_POST['content'];
} else {
$error .= "Your post should have content, you know, it wouldn't be a 'post' then.";
$abort = true;
}
if (!empty($_POST['category'])) {
$entry->c_Id = $_POST['category'];
}
if ($abort == false){
$this->postsRepository->update($entry);
$savedSuccess = true;
}
} else {
$error = "You have no permission to do this, how the hell did you get here?";
}
$this->render("post/admin/edit", [
'entry' => $entry,
'error' => $error,
'savedSuccess' => $savedSuccess,
'categoryFId' => $categoryFId
]);
}
I really hope someone can help me with this, I don't know what I could to to let them only disappear when the POSTS have already been send..
You have to check if there was a POST action use:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
in your case
...
if ($this->loginService->check()) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['title'])) {
...
}
}
}
I have that code and it work fine:
if (isset($_POST['submit1']))
{
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
include_once('./token.php');
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php'); //**PAGE WITH SUBMIT1**
}
token.php
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
The form in my1page.php contains:
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>">
Now i need to nest a second if isset submit (token must be unset in the last submit).
WHAT I TRIED WITHOUT SUCCESS
if(isset($_POST['submit'])){
$_SESSION['submit']=true;
}
if (isset($_POST['submit']) || ( isset($_SESSION['submit']) && $_SESSION['submit'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit1'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit']=false;
include_once('./script/token.php');
include_once('./my3page.php');
} else {
header("location: ./3.php");
}
}
include_once('./my2page.php');
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
HTTP works stateless. That means that what is happening here is the following:
User calls this page for the first time. He sends a GET request so isset($_POST['submit1']) is false.
Now he clicks on submit and sends the first POST request. (I assume that you set a value for submit1 there.) isset($_POST['submit1']) is true and my2page.php gets returned.
He sends the third request. Again a POST request, but this time with a value for submit2. Your server template engine starts evaluating the php. isset($_POST['submit1']) is false, so it returns the old my1page.php
Basically, don't nest your checks, but use a it else instead. (Think of it as a switch/case
isset($_POST['submit1']) → ./my2page.php
isset($_POST['submit2']) → //end page
none → ./my1page.php
You can't have 2 submits in the same time so what happens here is
if(condition){
if(!condition){
//do somthing
}
}
this will never works try to use another page or i advice to save the first submit in the $_SESSION;
ADD this lign
$_SESSION['submit1'] = (isset($_POST['submit1']))? true: false;
than change the first condition
if (isset($_POST['submit1']) || $_SESSION['submit1']) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
//DO SOMETHINGS
} else {
header("location: ./index.php");
}
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
This is how it should be;
if(isset($_POST['submit1'])){
$_SESSION['submit1']=true;
}
if (isset($_POST['submit1']) || $_SESSION['submit1']) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
//DO SOMETHINGS
} else {
header("location: ./index.php");
}
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
Your close you could possible just change this
if (isset($_POST['submit1'],$_POST['submit2'])) { //check isset on both
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
// if($_SESSION['user_token'] == $_POST['user_token']) { <--redundant check
unset($_SESSION['user_token']);
//DO SOMETHINGS
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
Depending on if you want an AND or an OR the above is equivalent to this
if (isset($_POST['submit1']) && isset($_POST['submit2'])) {
Obviously if you want an or then just put it here
if (isset($_POST['submit1']) || isset($_POST['submit2'])) {
It's not clear if you are talking about 2 POST's that are separate or concurrent
I have never done combining ajax and session before. So far i have done something like this.
var sessionvar;
$.ajaxSetup({cache: false})
$.get('test.php' ,function (data) {
sessionvar = data;
alert(sessionvar);
var checkedCbs = $('sessionvar:checked');
if (checkedCbs.length === 4) {
alert("You can only select 3 books");
this.checked = false;
}
});
I want to try set the limitations based on session. Inside the test.php i have something like this
<?php
session_start();
if(isset($_POST['sBorrow']) && $_POST['action']){
if(isset($_SESSION['sBorrow']) && is_array($_SESSION['sBorrow'])){
$sborrow = $_POST['sBorrow'];
$set = $_SESSION['sBorrow'];
}
else{
$set = array();
}
if($_POST['action'] == "SET"){
array_push($set, $_POST['sBorrow']);
$_SESSION['sBorrow'] = $set;
}
else if($_POST['action'] == "UNSET"){
$unset = $_SESSION['sBorrow'];
if(($key = array_search($_POST['sBorrow'], $unset)) !== false) {
unset($unset[$key]);
$_SESSION['sBorrow'] = $unset;
}
}
}
//session_destroy();
if(isset($_SESSION['sBorrow'])){
$countses = count($_SESSION['sBorrow']);
echo $countses;
}
// nothing requested, so return all values
print json_encode($_SESSION);
?>
Inside these i have create some array to store something. Never mind that, i just want to know how to run an ajax with session. Im not sure if my codes is right for implementing that.
I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}
Can someone please help me, i am trying to incorporate this piece of code:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
to fit this piece of code as an else statement:
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
I have a table in my database that puts a users block status from 0 to 1. and if a user blocks someone and that user tries to access their profile then i am trying to make it so that the user goes to another page that says blocked. i am doing this through <?php include(.. ?>
At the moment i just tried putting this at the top of the page:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
and whilst it is working and including the page mod_blocked.php its also bringing up mod_profile.php which is the default profile page and overlapping. So basically if a users not blocked they should go to mod_profile.php and if a users blocked they go to mod_blocked.php.
can someone please show me where im going wrong and how to achieve this?
Here's the entire page of code:
<?php
$page_title = "Profile";
include('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
?>
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
<?php
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
}
else if (!isset($profile_id)) {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
else if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include("includes/mod_profile/mod_account.php");
}
?>
I'm not sure if I fully understand what you are up to, but when I got your question correct, you could do it the following way:
$page_title = "Profile";
include('includes/headerframe.php');
// stores the correct include file...
$toInclude = false;
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
// look up for blocked user..
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
// don't do the include inside the loop
// we simply set the $toInclude
$toInclude = "includes/mod_profile/mod_blocked.php";
break;
}
}
$user_info_set = get_user_info();
// in case no include is set, and there is no profile...
if ( !$toInclude && (!$user = mysql_fetch_array($user_info_set) ||
!isset($profile_id)) ) {
// we set the $toInclude now
$toInclude = 'includes/mod_profile/mod_noprofile.php';
}
if($toInclude) {
// either blocked or no-profile
include($toInclude);
} else {
// user is not blocked, and you have a profile
// proceed with your code for normal user handling here
}
Couple options here.
Add break or die() like such
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
break;
}
}
?>
Or, surround it in the else
<?php
$page_title = "Profile";
include ('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset($_GET['id'])) {
$profile_id = $_GET['id'];
die();
}
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_blocked.php");
} else {
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
} else if (!isset($profile_id)) {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include ("includes/mod_profile/mod_profile.php");
} else if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include ("includes/mod_profile/mod_account.php");
}
}
}
?>