I have been making a MVC site, I am having trouble sending the row id from my form to my controller.
The code that I am using for my form gets the row ID for each db entry and assigns it to a hidden value. When the form is submitted it sends the parameters to the controller (should send $uid) but the uid isn't making it to the controller.
Form Code (buttons.php)
<?php
$itemsDAO = new ItemsDAO();
$result = $itemsDAO->getItems();
foreach ($result as $row) {
$uid = $row['id'];
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Controller function
function deleteItem($parameters) {
$id=$parameters["fId"];
if ($this->model->deleteItem( $id )) {
$this->model->hasDeleteFailed = false;
$this->model->setDeleteItemConfirmation();
return (true);
}
else
$this->model->deleteItemError ( DELETE_ITEM_ERROR_STR );
}
View.php - where I am showing the list of db items and the buttons.php
$this->model->prepareItemList();
$buttons = file_get_contents( "templates/buttons.php");
$HTMLItemList = "";
foreach ( $this->model->itemList as $row )
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .$row ["description"] . " " . $buttons ."</blockquote></li>";
Try $_POST["fld"]; in your controller to get value. If you are using any framework like codeigniter then you can use its own methods.
For example codeigniter has
$this->input->post();
Okay,
step by step the $uid has to first make it into the form elements value attribute. Check the html source code to make sure this is actually happening.
place var_dump($_POST) exit; in your controller to find what is actually being recieved if anything at all.
check to make sure your result array element actual has a value and not an empty string value or NULL.
Hmm S.O. code formatting bad.
// turn on error reporting for dev to view empty or missing variable errors
ini_set('error_reporting', E_ALL);
$result=$itemsDAO->getItems();
foreach ($result as $row) {
($row['id'] != ''? $uid = $row['id'] : $uid ='no id found');
// debug result
echo '<pre>' . print_r($row,1) .'</pre>';
}
Related
Sorry in advance if this is a really stupid mistake, but I appreciate any help that can be given.
This is the code that initialise that data in the form. They are radio buttons that are populated via an SQL database.
echo "<form method='post' action='home.php'>";
$result = queryMySQL("SELECT Name FROM Quotees");
if ($result->num_rows == 0) {
$error = "<span class='error'>No people can be found</span><br><br>";
} else {
$numrow = $result->num_rows;
for ($count = 1; $count <= $numrow; $count++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
$name = stripslashes($row['Name']);
echo '<input type="radio" name="choice" value="' . $name . '" />' . $name;
echo '<br>';
}
}
Later on in the code I try to access the value picked using the POST method, as follows:
$personPicked = stripslashes($_POST["choice"]);
if ($person === $personPicked) {
//code
} else {
echo "wrong answer";
}
The error I receive is "Undefined index: choice" on the line where the POST method is called.
I submit the form using the following code out side of the php segment as HTML code
<div class="confirmButton">
<form action="home.php">
<input type="submit" value="Confirm">
</form>
Thanks for any help you can give as to why my code isn't working/how I can go about solving the problem.
The submit button is in a different form.
When you click it, you submit the form that does not include the radio buttons.
You need to put the submit button and the radio buttons in the same form.
Okay the following script is functional but not very good.
I am posting data in array and in bulk to a exec command.
Everything works and after some time I get an array back with my results. This is the problem it takes a very long time.
I would prefer to see the results as they are fetched so I would like to see the array come together line by line in my PHP script instead of one big block of data.
This is my code:
<html>
<body>
<form method="post">
<div align="center">
<textarea name="mp" cols="60" rows="10">0|1|2</textarea>
<br />
Delim:
<input type="text" name="delim" value="|" size="1" />
data1:
<input type="text" name="mail" value="0" size="1" />
data2:
<input type="text" name="prx" value="1" size="1" />
<input type="submit" value=" send " name="btn-submit" />
</div>
</form>
</body>
</html>
<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
if (isset($_POST['mp'], $_POST['delim'], $_POST['btn-submit'], $_POST['mail'], $_POST['prx'])) {
$mps = preg_split('/\r\n|\r|\n/', $_POST['mp']);
// Create an array to store results
$result_data = array();
// Iterate over requests
foreach ($mps as $mp) {
$mp = explode($_POST['delim'], $mp);
// Store the account details in variables
$email = $mp[$_POST["mail"]];
$proxy = $mp[$_POST["prx"]];
// Prepare a reusable string
$result_string = "/usr/local/bin/casperjs test.js \"" . $email. "\" \"" . $proxy . "\" 2>&1";
// Add result to the array
$result_data[] = $result_string;
}
// Store of results complete. We now display them.
if (count($result_data)) {
foreach ($result_data as $result_data_entry) {
echo exec($result_data_entry);
}
echo "</ul>";
}
}
?>
How can I do this?
You can't do this in PHP. PHP is executed all server-side, not client side. That means that the results are all found before sending it to the user. You could, however, use Javascript to do this. I'm not sure how you would do it, but there's probably a way to dynamically see the results of a query.
You can use the Symfony Process component which supports incremental output.
Example:
foreach ($mps as $mp) {
// ...
$result_string = new Process("/usr/local/bin/casperjs test.js \"" . $email. "\" \"" . $proxy . "\" 2>&1");
$result_data[] = $result_string;
}
// Store of results complete. We now display them.
if (count($result_data)) {
while (count($result_data_entry) > 0) {
foreach ($result_data_entry as $i => $process) {
if (!$process->isStarted()) {
$process->start();
continue;
}
echo $process->getIncrementalOutput();
if (!$process->isRunning()) {
unset($result_data_entry[$i]);
}
}
sleep(1);
}
}
Have you tried using
flush();
after
echo exec($result_data_entry);
?
I am trying to save form data to an array so I can display it in a table later. The problem I am having is that when I click the submit button and it reloads using php_self it seems to initialize the variables everytime. Here is an example of what I am trying to do.
<?php
// if first time initialize variables
if (!isset($i)) {
echo "in initialize section<br />";
$i = 0;
$itemno[] = "";
$desc[] = "";
}
if (isset($_POST['submitbtn'])) {
$itemno[$i] = $_POST['item'];
$desc[$i] = $_POST['desc'];
echo "Item# = " . $itemno[$i] . "<br />";
echo "Desc. = " . $desc[$i] . "<br />";
$i += 1;
echo "i = $i";
var_dump($itemno);
var_dump($desc);
}
?>
<form id="submititem" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="submit" >
<input name="item" placeholder="Enter item #" size="18" />
<input name="desc" placeholder="Enter Description" size="18" />
<input name="submitbtn" type="submit" value=">">
</form>
Thanks
Ralph
The issue you're having is that PHP starts "fresh" with every page load. You'll need to store the data somewhere if you want to preserve your array over several submissions. Session or a database are two of the commonest ways of doing this.
Here's how you might do it with session:
<?php
session_start();
$items = isset($_SESSION['items']) ? $_SESSION['items'] : array();
$descriptions = isset($_SESSION['descriptions']) ? $_SESSION['descriptions'] : array();
// your logic here...
$_SESSION['items'] = $items;
$_SESSION['descriptions'] = $descriptions;
Note that if your array is likely to get very big or if you have very many users, you'll probably want to use a database to store the item/description information.
You have to deal with session
$_SESSION['itemno'][$_SESSION['i']] = $_POST['item'];
I've created a textbox so when the admin types a name and clicks submit, it will shows a list of retrieved data from the database.
<form method="post" action="">
<?php
$teacherName = $_POST['teacherName'];
if ($_POST['submitted'] == 1) {
if($teacherName != ""){
$getName = mysql_query("SELECT name, user_id FROM members WHERE name = '$teacherName'");
$teacherdetails = mysql_fetch_array($getName);
$teachername = $teacherdetails['name'];
$teacher_id = $teacherdetails['user_id'];
if($teachername != ""){
print $teachername . "<br/>";
} else {
print "Give a valid name <br/>";
}
}
}
if ($teachername == ""){ ?>
Teacher name:<input type="text" size="20" name="teacherName"><input type="hidden" name="submitted" value="1"><br/>
<input type="submit" value="Submit" />
<?php $getModule = mysql_query("......");
while ($row2 = mysql_fetch_array($getModule)) { ?>
<input type="checkbox" name="modules[]" value="<?php print $row2["module_id"]?>"/> <?php print $row2["module_name"] . '<br/>'; } ?>
</div><br/> <?php } ?>
<input type="submit" value="Submit" />
</form>
Below I wrote this code (in the same script):
<?php
$modules = $_POST['modules'];
for($i = 0; $i < count($modules); $i++){
$module=mysql_query("INSERT INTO module (module_id,user_id) VALUES ('$modules[$i]','$teacher_id')");
}
?>
but for some reason when I call the variable "$teacher_id" (which is the value I retrieved before from the database. It works fine in the form) it returns nothing. It's null but I can't understand why.
Am I doing something wrong?
First off, put the PHP outside the form tags, at the top.
Secondly, the data you are receiving could be an array; with more than one result set;
do this just incase it it returned as that;
foreach($teacherdetails AS $teacher) {
//also set the values of the variables here
echo $teacher['name'];
echo $teacher['user_id'];
}
Regarding the last bit, is the $teacher_id successfully printing a result?
Also, where is the modules being input and posted from?
I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.
I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.
On form submit, I include the php file that submits this data to the database:
<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<input type="submit" value="Submit!" />
</form>
The variable $name comes from this (which populates my dropdown menu):
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
echo "</select>";
And here is my formsubmit.php:
<?php:
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
header("Location: quotes.php");
if (#mysql_query($sql)) {
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.
edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):
file 1:
<form action="formsubmit.php" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
formsubmit.php:
<?php
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$name = $_POST['name'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
$nyme = htmlspecialchars($temp['name']);
echo "<option value='$nyme'>$nyme</option>";
}
echo "</select>";-
This way you will receive the value of the name in $_POST array
and you have to get that value out of $_POST array as well you need to change the
code add the following line to get the name in your script.
$name = $_POST['name'];
you need to change the form action tag
<form action='formsubmit.php' .....>
and in that file after successful insertion you can redirect the user to whereever.php.
so it was fun explaining you every thing bit by bit change this now in your code as well.
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}