Is there a specific way to read POST data from wpforms? - php

I'm trying to access form data entered by the user on a form created with wpforms and save them as session variables, but no matter what I try, I can't seem to get anything from the form. I can hardcode the information into the session variables, and they work as expected, but it needs to pull the data from the form.
Here's the snippet from the php code I have on the page:
<?php
echo session_id();
$_SESSION['age'] = $_POST['wpforms[fields][3]'];
$_SESSION['gender'] = $_POST['wpforms[fields][2]'];
?>
I've also included some code from the second page once the form has been submitted, which is where I see that the session variables are set correctly when I hard code them in the first page, but not set when I try to pull from the form.
echo session_id();
if ( isset($_SESSION['gender'])) {
echo "Gender is set.<br>";
}
else {
echo "Gender not set<br>";
}
if ( isset($_SESSION['age'])) {
echo "Age is set.<br>";
}
else {
echo "Age not set.<br>";
}
echo $_SESSION['gender'];
echo $_SESSION['age'];
Any insight will be greatly appreciated! Thanks!
Screenshot of page inspection

I figured it out. I had to add the following code to my functions.php file on the wordpress theme I was using:
add_action('init', 'register_my_session');
function register_my_session() {
if (!session_id() ) {
session_start();
}
function wpf_dev_process_entry_save( $fields, $entry, $form_id, $form_data ) {
// Only run on my form with ID = 4117
if ( absint( $form_data['id'] ) !== 4117 ) {
return $fields;
}
$_SESSION['age'] = $fields[3]['value']; // This is where we access the form data
$_SESSION['gender'] = $fields[2]['value'];
}
add_action( 'wpforms_process_entry_save', 'wpf_dev_process_entry_save', 10, 4 );
}

Related

after form submit,if user already login then form will submit else login first then form should be submit automatically

I am trying to implement a form, if user already login then from will submit, else login first then form should be submit automatically with the help of cookie an error is sowing in my code on last move_uploaded_file.
My code is given as follows
<?php
if(isset($_POST['submit'])
{
$ad_title=$mysqli->real_escape_string($_POST['ad_title']);
$category=$mysqli->real_escape_string($_POST['category']);
$sub_category=$mysqli->real_escape_string($_POST['sub_category']);
$description=$mysqli->real_escape_string($_POST['description']);
$rent_amount=$mysqli->real_escape_string($_POST['rent_amount']);
$rent_security=$mysqli->real_escape_string($_POST['rent_security']);
$contact=$mysqli->real_escape_string($_POST['contact']);
$email=$mysqli->real_escape_string($_POST['email']);
$city=$mysqli->real_escape_string($_POST['city']);
$state=$mysqli->real_escape_string($_POST['state']);
$area=$mysqli->real_escape_string($_POST['area']);
$buy=(isset($_POST['buy'])?1:0);
$sell=(isset($_POST['sell'])?1:0);
$rent=(isset($_POST['rent'])?1:0);
$manufacture=$mysqli->real_escape_string($_POST['company_name']);
$conditions=$mysqli->real_escape_string($_POST['condition']);
$rent_option=$mysqli->real_escape_string($_POST['rent_option']);
$a=$_FILES['file']['name'];
$path="image/product/$a";
$b=$_FILES['file2']['name'];
$path2="image/product/$b";
$c=$_FILES['file3']['name'];
$path3="image/product/$c";
$d=$_FILES['file4']['name'];
$path4="image/product/$c";
$e=$_FILES['file5']['name'];
$path5="image/product/$c";
if(isset($_SESSION['user_id'])){
$query=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)values('$ad_title','$category','$sub_category','$description','$a','$b','$c','$d','$e','$city','$state','$rent_amount','$rent_option','$rent_security','$contact','$email','$area','$buy','$sell','$rent','$user_id','$manufacture','$conditions')");
move_uploaded_file($_FILES['file']['tmp_name'],$path) & move_uploaded_file($_FILES['file2']['tmp_name'],$path2) & move_uploaded_file($_FILES['file3']['tmp_name'],$path3) & move_uploaded_file($_FILES['file4']['tmp_name'],$path4) & move_uploaded_file($_FILES['file5']['tmp_name'],$path5);
if($query)
{
echo "success";
}
}else{
$time = time() + 60;
setcookie('email',$email,$time);
setcookie('ad_title',$ad_title,$time);
setcookie('category',$category,$time);
setcookie('sub_category',$sub_category,$time);
setcookie('description',$description,$time);
setcookie('rent_amount',$rent_amount,$time);
setcookie('rent_security',$rent_security,$time);
setcookie('contact',$contact,$time);
setcookie('city',$city,$time);
setcookie('state',$state,$time);
setcookie('area',$area,$time);
setcookie('buy',$buy,$time);
setcookie('sell',$sell,$time);
setcookie('rent',$rent,$time);
setcookie('manufacture',$manufacture,$time);
setcookie('condition',$conditions,$time);
setcookie('rent_option',$rent_option,$time);
setcookie('file',$a,$time);
setcookie('file2',$b,$time);
setcookie('file3',$c,$time);
setcookie('file4',$d,$time);
setcookie('file5',$e,$time);
header("Location:product/login.php"); }
}
if(isset($_COOKIE['email'])){
$email =$_COOKIE['email'];
$cookie2 = $_COOKIE['ad_title'];
$cookie3 = $_COOKIE['category'];
$cookie4 = $_COOKIE['sub_category'];
$cookie5 = $_COOKIE['description'];
$cookie6 = $_COOKIE['rent_amount'];
$cookie7 = $_COOKIE['rent_security'];
$cookie8 = $_COOKIE['contact'];
$cookie9 = $_COOKIE['city'];
$cookie10 = $_COOKIE['state'];
$cookie11 = $_COOKIE['area'];
$cookie12 = $_COOKIE['buy'];
$cookie13 = $_COOKIE['sell'];
$cookie14 = $_COOKIE['rent'];
$cookie15 = $_COOKIE['manufacture'];
$cookie16 = $_COOKIE['condition'];
$cookie17 = $_COOKIE['file'];
$cookie18 = $_COOKIE['file2'];
$cookie19 = $_COOKIE['file3'];
$cookie20 = $_COOKIE['file4'];
$cookie21 = $_COOKIE['file5'];
$cookie22 = $_COOKIE['rent_option'];
$pat="image/product/$cookie17";
$pat2="image/product/$cookie18";
$pat3="image/product/$cookie19";
$pat4="image/product/$cookie20";
$pat5="image/product/$cookie21";
$query1=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)
values
('$cookie2','$cookie3','$cookie4','$cookie5','$cookie17','$cookie18','$cookie19','$cookie20','$cookie21',
'$cookie9','$cookie10','$cookie6','$cookie22','$cookie7','$cookie8',
'$email','$cookie11','$cookie12','$cookie13','$cookie14','$user_id','$cookie15','$cookie16')");
move_uploaded_file($cookie17,$pat)&
move_uploaded_file($cookie18,$pat2)&
move_uploaded_file($cookie19,$pat3)&
move_uploaded_file($cookie20,$pat4)&
move_uploaded_file($cookie21,$pat5);
if($query1){
echo "Succes";
}
else{
echo "Something went wrong.";
}
}
?>
Not a solution to your question: You have no name assigned to your select menu.
That aside, you need to check for the existence of a session variable - assuming you set some sort of session variable when the user logged in? ie: $_SESSION['username']='fred' etc? You can generate javascript that will submit the form at page load.
<form name="appointment" method="POST" action="appointment.php" role="form">
<!-- form contents -->
</form>
<?php
if( isset( $_SESSION['username'] ) ){
echo "
<script type='text/javascript'>
document.querySelectorAll( form['name=\"appointment\"]' )[0].submit();
</script>";
}
?>

php echo wordpress user meta data

On my site when a user is registering there is an option to pick from an additional option that pears their account up with a non-profit. Once the user has registered and viewing specific pages of the site I want the site to be tailored to them using php that grabs their meta info. For this I will echo a button that tailors the front-end based on what meta value they have selected when registering.
If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'nch',true); //stores the value of logged in user's meta data for 'National Coalition for the homeless'
$havemeta2 = get_user_meta($userID,'rotary-international',true); //stores the value of logged in user's meta data for 'Rotary International'
$havemeta3 = get_user_meta($userID,'khan-academy',true); //stores the value of logged in user's meta data for 'Khan Academy'
$havemeta4 = get_user_meta($userID,'wwf',true); //stores the value of logged in user's meta data for 'World Wildlife Fund (WWF)'
$havemeta5 = get_user_meta($userID,'bcrf',true); //stores the value of logged in user's meta data for 'The Breast Cancer Research Foundation'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div <p>nch</p> class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div <p>rotary-international</p>class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div <p>khan-academy</p>class="Button3"></div>
<?php } elseif ($havemeta4) { ?>
<div <p>wwf</p>class="Button4"></div>
<?php } elseif ($havemeta5) { ?>
<div <p>bcrf</p>class="Button5"></div>
<?php } else { ?>
<div><p>None - No Matching Affiliation</p></div>
<?php }?>
-----------------------New Code----------------------
This allows me to see what the affiliation variable is pulling for the user
The result is this: 'User Affiliation: khan-academy'
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>
can you pass the meta into a session var, ie $_SESSION['havemeta5'];
get_user_meta is set up wrong. Try this:
$havemeta1 = get_user_meta( $userID, 'nch', true );
...and so on. You need to pass the $userID as the first parameter of get_user_meta() - not $affiliation.
update
If your user meta is 'affiliation' (which allows you to call $current_user->affiliation), you can do something like this:
<?php
global $current_user;
get_currentuserinfo();
$userID = $current_user->ID;
$affiliation = get_user_meta( $userID, 'affiliation', false );
$affiliation = $affiliation[0];
if( 'nch' == $affiliation ){
print '<div class="Button1"><p>nch</p></div>';
} elseif( 'rotary-international' == $affiliation ){
print '<div class="Button2"><p>Rotary International</p></div>';
} elseif( 'khan-academy' == $affiliation ){
print '<div class="Button3"><p>Khan Academy</p></div>';
} elseif( 'wwf' == $affiliation ){
print '<div class="Button4"><p>wwf</p></div>';
} elseif( 'bcrf' == $affiliation ){
print '<div class="Button5"><p>bcrf</p></div>';
} else {
print '<div><p>None - no matching affiliation</p></div>';
print '<div>User ID: '.$userID.', Affiliation: '.$affiliation.'</div>';
}

Drupal login logout links

I've got the following code to show when a user is logged in and when they're not.
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
} else {
echo "You are not logged in!<br>";
}
?>
I want to add a logout link (when logged in) & and a login link (when not logged in). Would I just use html < a > links? If so, how would I incorporate them?
In most cases you would just use a regular menu and add the two items.
The visibility of the items is controlled by drupals menu-system, so you won't see the login-link if you're already logged in and you won't see the logout-link if you're not logged in.
If you want to output the links programmatically I would recommend using the "l" function
l($text, $path, array $options = array())
To check if a user is logged in you can simply use the user_is_logged_in function provided by the users module.
So your code would become
<?php
if( user_is_logged_in() ) {
print l('logout', 'user/logout');
}
else {
print l('login', 'user/login');
}
Depends on where you want to show the links. If it is on the same page/piece of code, just go ahead and echo them:
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
echo "Logout";
} else {
echo "You are not logged in!<br>";
echo "Login";
}
?>
You can simply use the url function of Drupal core as below
<?php
global $user;
if ($user->uid > 0) {
echo 'Logout ';
} else {
echo 'Logout ';
}
?>
Hope this will help you :)

Page should be reloaded on submitting the button

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.

WP Custom Fields, hide code if field is empty?

I'm using a custom field template plugin for WP. I want to hide
<li>Sqft: [squareft]</li>
if the field is empty. I've tried different codes, these are two I've tried based on suggestions:
<?php if ('squareft' !== '') { ?><li>Sqft: [squareft]</li>
<?php } ?>
And
<?PHP $squareft = ('squareft'); if ($squareft != '') { echo '<li>Sqft: [squareft]
</li>';} if (empty($squareft)) { echo " "; } ?>
I obviously don't have a clue what I'm doing, although I'm learning through trial and error. It uses shortcodes, so [squareft] is what to use to output the field data.
Any help is appreciated.
Update:
I think I've got it working, based on doing this method. Not yet gone live but it's working in my test post.
<?php
global $post;
$bathrooms = get_post_meta($post->ID, 'bathrooms', true);
if ( !empty($bathrooms) ) { echo '<li>Baths: [bathrooms]</li> | ' ; }
?>
Try this (replacement of the if statement in your first code sample):
<?php if (do_shortcode('[squareft]') != '') { ?>
<li>Sqft: [squareft]</li>
<?php } ?>
Or if you know the custom meta field's actual field name (generated by the plugin, probably some kind of prefix + squareft) you can do:
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { ?>
It would help to know the specific plugin you are using.
Using the get custom fields plugin I have used the following to hide content if the field it empty (in this case the 'info' field):
<?php $info = c2c_get_custom('Info');?>
<?php if ( $info == "" ) {echo "";} else {echo "<h2 id=comments>Notes</h2><div id=info>$info</div>" ;} ?>
Not sure if this will help but it might give someone else more ideas.
Are tring this :
<?php if ( (c2c_get_custom('image')) ) { ?>
blah blah blah php code etc...
<?php } ?>
Works perfectly. If field has something it shows, if not no show, it was leaving a broken image when empty, now its good.
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { echo "display output";?>

Categories