Yii posting data to controller turns up empty - php

Explanation
I have a plain HTML form with a few fields
I post it to my controller function
In the function I var_dump the POST data
public function actionReceive()
{
echo "<pre>";
var_dump($_POST);
echo "</pre>";
exit();
}
On screen php shows the POST data is empty
In Firebug I can see the POST data
Question
Why is the POST data not displayed in the var_dump?
When I post it directly to a view file in site/page the POST is displayed with var_dump.

why are you using var_dump($_POST) ??
If you want to pass data from one page to another in PHP just create 2 files. Let's sets first file name as send.html, and another one as receive.php.
Now put these codes into send.html file:
<form method="post" action="catching-var.php">
1. <input type="text" name="name1"/><br/>
2. <input type="text" name="name2"/><br/>
3. <input type="text" name="name3"/><br/>
4. <input type="text" name="name4"/><br/>
<input type="submit" name="submit"/>
</form>
Also put these lines into receive.php file:
<?php
$name0 = $_POST['name0'];
$name1 = $_POST['name1'];
$name2 = $_POST['name2'];
$name3 = $_POST['name3'];
$name4 = $_POST['name4'];
echo $name0.'<br/><br/>';
echo $name1.'<br/><br/>';
echo $name2.'<br/><br/>';
echo $name3.'<br/><br/>';
echo $name3.'<br/><br/>';
?>
Put some values into the HTML textbox fields and click the Submit button.
EDIT: If you do this in Yii Framework all of these operations and codes are same. But you must paste the PHP codes in action. That's all.
Please tell me; in which step you have problem?
Thanks. Best regards.

Related

Using $_GET in the same file of text field that I GET from

Not sure if I titled it right, however my problem is stated below and I can't, for the life of me, figure it out.
In the same file, write a form consisting of a single text field and a
submit button. The “action” attribute to the form should be the same
page that the form is on (don’t hard code, use $_SERVER[‘PHP_SELF’]).
The form should send the contents of the text field via GET.
Upon submitting the form, you should be redirected to the same page,
but the URL should contain the string from the text field as a GET
request normally behaves.
<!-- I am supposed to pass the value of the text
field over to the url according to the question -->
<form action="questionThree.php" method="get">
<input type="text" name="text"><br>
<input type="submit">
</form>
Have trouble getting the code to display properly. They're supposed to be in html tags, although it's a php file.
Im baffled as to why you're passing it somewhere else however..
I think I have made sense from what you're asking so here goes...
On the submit input field add name="send"
then the HTML and PHP (for the same page) would be:
<form action="" method="get">
<input type="text"><br />
<input type="submit" name="send">
</form>
<?php
if (isset($_GET['send']))
{
$sentence = strtolower("My name is Justin and I am learning PHP programming and C++ programming");
function countWords($sentence)
{
//Using 'explode' to split words, thereby creating an array of these words
$wordsArr = explode(' ', $sentence);
$vals = array_count_values($wordsArr);
echo "<pre>";
print_r($vals);
echo "</pre>";
foreach ($vals as $key => $value)
{
echo "<b>" . $key . "</b> occurs " . $value . " times in this sentence <br/>";
}
}
countWords($sentence);
}
?>
Though I still see no purpose of the input text field?

Send form elements to image generation function

I'm completely up against a wall with this. I've been trying to learn PHP from tutorials to output two form inputs to my function to generate an image.
I've fixed an issue with my mixing up POST and GET but now my script doesn't function at all and I cant understand why - I'm sure this is hilds play for any developer but I'm in a bit over my head.
This is my app.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from text input.
$colour = $_POST['colour']; // data from colour radio.
}
?>
...
<form action="/app.php" method="post">
<input type="text" name="name"/>
<input name="colour" type="radio" value="1">Red<br>
<input name="colour" type="radio" value="2">Blue<br>
<input name="colour" type="radio" value="3">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print $data;?>" alt="">
Calls pngfile.php
<?php
require_once 'functions.php'; // Requires and includes do not need brackets.
$textdata = $_POST['data'];
$colourdata = $_POST['colour'];
process($textdata,$colourdata);
exit;
?>
Which in turn calls functions.php
<?php
/* Generate Image */
function process($textdata, $colourdata)
{
...
All of this was working perfectly before but the only change I have added is updating all elements to use POST and also adding in the code across the three files to add the selected colour to post. However with this tweaked code, I get no image output, even though I know my main image function works fine, so it must be my app.php and pngfile.php at fault.
Can anyone please give me some guidance on where I am going wrong?
Your problem is that you're sending this:
<img src="pngfile.php?data=<?php print $data;?>" alt="">
But your code is looking for this:
$textdata = $_POST['data'];
$colourdata = $_POST['colour'];
There's no post, and there's certainly no $_POST['colour']. There is a $_GET['data'] however; I think that's what you're looking for. Data passed as part of the URL is part of a GET request, and is available in $_GET. $_POST is for data sent with a POST request.

Using a variable across the pages in PHP [duplicate]

My website involves a user submitting data over several pages of forms. I can pass data submitted on one page straight to the next page, but how do I go about sending it to pages after that? Here's a very simplified version of what I'm doing.
Page 1:
<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>
Page 2:
<?php
$name=$_POST["Name"];
echo "Hello $name!<br/>
<form action='page3.php' method='post'>
Please enter your request: <input type='text' name='Req'/>
<input type='submit' value='Submit'/></form>";
?>
Page 3:
<?php
echo "Thank you for your request, $name!";
?>
The final page is supposed to display the user's name, but obviously it won't work because I haven't passed that variable to the page. I can't have all data submitted on the same page for complicated reasons so I need to have everything split up. So how can I get this variable and others to carry over?
Use sessions:
session_start(); on every page
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
You could store the data in a cookie on the user's client, which is abstracted into the concept of a session. See PHP session management.
if you don't want cookies or sessions:
use a hidden input field in second page and initialize the variable by posting it like:
page2----
$name=$_POST['name']; /// from page one
<form method="post" action="page3.php">
<input type="text" name="req">
<input type="hidden" name="holdname" value="<? echo "$name"?>">
////////you can start by making the field visible and see if it holds the value
</form>
page3----
$name=$_POST['holdname']; ////post the form in page 2
$req=$_POST['req']; ///// and the other field
echo "$name, Your name was successfully passed through 3 pages";
As mentioned by others, saving the data in SESSION is probably your best bet.
Alternatly you could add the data to a hidden field, to post it along:
page2:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
echo "hello $_POST['username'};
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
Remember, if you aren't using prepared statements, make sure you escape all user input...
Use SESSION variable or hidden input field
This is my workaround of this problem: instead of manually typing in hidden input fields, I just go foreach over $_POST:
foreach ($_POST as $key => $value) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
Hope this helps those with lots of fields in $_POST :)

Add data to sql on button click

my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>

Basic problem of getting values of my textbox

I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"

Categories