How to assign posted values to a variable in cakephp? - php

I likes to assign posted values to a variable,
In view i wrote the code,
<?php echo $this->Form->create('Register'); ?>
<fieldset>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('Confirm password');
?>
</fieldset>
Form->end(__('Submit'));
and in controller,
if($this->request->is('post')){
//print_r($this->data);
$uname = $this->request->data('username');
echo "uname".$uname;
exit;
}
The problem is that i didnt get the value on $uname.

Make sure your debug is enabled. Use this - pr($this->request->data); to see what's in that variable. The CakePHP form naming data usually looks something like this
$this->request->data['Formname']['fieldname']
In your case it should be
$this->request->data['Register']['username']

please try using this code
$this->params['form']['YOUR_VARIABLE_NAME']
or
$this->data['FORMNAME']['YOUR_VARIABLE_NAME']

Related

Get input from HTML form in PHP

I'm learning MySQL and PHP and got a problem with the input of the form. So I wrote a small test code, but it still cannot work. My PHP version is 5.6.
The code:
<html>
<body>
<form action ="2.php" method ="post">
Name: <input type="text" name="username" />
<input type ="submit" value="ok" />
</form>
</body>
</html>
and
<html>
<?php
if(isset($_POST['username'])){
$user=$_POST['username'];
echo $user;
echo " is your name";
}
else{
$user=null;
echo "error";
}
?>
</html>
The output of the project is always error, can't output the input before.
I tried single quote and double quote for username, both can't work.
I also tried to set always_populate_raw_post_data in php.ini to 0, -1, 1, all can't work.
I don't know where the problem is, though it might be very silly.
As what it look it is correct and should run without any problem. Make sure the above code is what you actually have. From my experience most of the form submission can be
you don't have correct name (username)
you might send incorrect http verb (post)
you submit to wrong endpoint (2.php)
From you code above everything look fine. if you still don't have the right result, you better debug it with var_dump, or print_r function with these built-in
$_POST, $_GET, $_REQUEST and check whether they contains you form variable name username
You are using isset as a variable, but it is a function that returns a boolean.
Change $user=isset($_POST['username']); to $user=$_POST['username'];
Another thing is that in both case you will end up in the IF condition even if there is no value added to the field so you can do something like this too:
<html>
<?php
if(isset($_POST['username']) && !empty($_POST['username'])){
$user=$_POST['username'];
echo $user;
echo " is your name";
}
else{
$user=null;
echo "error";
}
?>
</html>

Pass a Variable from one file to another in WordPress

I have added a custom field in a post (wp-themes/template/modal-question.php) like this
<input type="checkbox" name="anonymous" value="true">
<span><?php _e('Go Anonymous', ET_DOMAIN); ?></span>
and variable is
<?php if(isset($_POST['anonymous1']))
{ $post_author = 3; echo $post_author; }
?>
I need to pass this varible '$post_author' ($_GET or $_POST) into another file post.php (Insert query into DB file), which resides in wp-includes/post.php
Please let me know if there's a way to do it.
Thanks in advance.
Here you go:
One time create this:
$GLOBALS['anonymous1'] = 'Your Value';
Then you get in any template of WordPress
echo $GLOBALS['anonymous1'];
Edit:
<?php
if(isset($_POST['anonymous1'])) {
$GLOBALS['anonymous2'] = $_POST['anonymous1'];
echo $GLOBALS['anonymous2'];
}
?>

I want to click on an html link and echo it in php

I cant get it work. Need something really simple, not complicated. And of course not using form and button.
I have a link:
click here
my php script has something like:
<?php
echo $_POST['name'];
echo $_POST['email'];
?>
Ok. So how can I post variables using post in an href? Is it possible?
Variables added to the Query string are found in $_GET, not in $_POST.
You want to use $_GET['field_name']
<?php
echo $_GET['name'];
echo $_GET['email'];
?>
$_POST is populated from POST request body data. You don't have a POST request here.
$_GET is populated from URL query parameters. You have URL query parameters here.
echo $_GET['name'];
echo $_GET['email'];
W3Schools has an example like this (http://www.w3schools.com/PHP/php_forms.asp):
<?php echo $_POST["name"] ?>
<?php echo $_GET["name"] ?>

Cannot get posted variables to echo in the form

It seems my code is correct, however the posted variables in the form will not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show.
I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db); comes from my init.php file that is called at the beginning of the file below.
when I
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
Hoi Stephen,
Try print_r($_POST["first_name"]); instead of var_dump();
or just for all:
print_r($_POST);
best regards ....
add this at the top of your html page
#extract($_REQUEST);
and put is just to check and after checking remove the below line
print_r($_REQUEST);
hope this help .

How do I print the username using session in cakephp?

I cannot get my username to show on the website when I call it form the session.
The code on the index page is:
<h1>Users Home</h1>
Welcome <?php $user = $this->Session->read('Users.username');?>
What am I doing wrong?
I've also tried various other ways of calling it and then getting different error messages.
In your code you are setting $user with the content of the username. But you are not printing it.
<h1>Users Home</h1>
Welcome <?=$this->Session->read('Auth.User.username')?>
which is short for
<h1>Users Home</h1>
Welcome <?php print $this->Session->read('Auth.User.username'); ?>
As Hugo said you should do the following:
<h1>Users Home</h1>
Welcome <?php echo $this->Session->read('Auth.User.username'); ?>
If you are going to use it on more than one view, I would suggest you to add the following on AppController.
public function beforeFilter() {
$this->set('username', AuthComponent::user('username'));
// OR $this->set('username', $this->Session->read('Auth.User.username'));
}
And then on any of your view just use the variable $username to print the current username.
<?php echo $username; ?>
After getting $user, you should do a echo $user or print_r($user) and you'll get the username printed.
echo $this->Session->read('Auth.User.username');
Or if you are using Cakephp 2.x you can use AuthComponent::user('username') anywhere in your code.

Categories