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);
?
Related
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>';
}
I'm using PHP's Rand() function to generate two random numbers that I can compare against the user's, and echo out a success message if the user's answer equals (1st random number + 2nd random number.) However, I'm running into problems.
I suspected that the form was re-generating the numbers every time the form POSTED and the input was collected, so I tried using sessions instead to keep those numbers persistent. It's a mess to say the least.
I found this existing post: Problems with guess a number game , but the solution didn't remedy my problem.
<?php
if(empty($_POST))
{
$_SESSION['$number1'] = Rand(0, 100);
$_SESSION['$number2'] = Rand(0, 100);
}
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == ($_SESSION['$number1'] + $_SESSION['$number2']))
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
echo "<hr><br> What is... <b>" . $_SESSION['$number1'] . " + " . $_SESSION['$number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($_SESSION['$number1'] + $_SESSION['$number2']);
?>
Any help would be appreciated!
I changed a few things, personally, I wouldn't use the session, rather user hidden inputs. (if you're worried about security.. you shouldn't be.. numbers game, not banking site)
<?php
//Create a function to generate the random numbers (So we can re-use it)
function generateNumbers()
{
$one = Rand(0, 100);
$two = Rand(0, 100);
//Now return the random numbers
return array('number1' => $one, 'number2' => $two);
}
//Check if the submit button has been clicked
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == $_POST['number1'] + $_POST['number2'])
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
//Now we create the numbers
$numbers = generateNumbers();
echo "<hr><br> What is... <b>" . $numbers['number1'] . " + " . $numbers['number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
<!-- Personally I would rather use hidden inputs, than use the session -->
<input type="hidden" name="number1" value="<?php echo $numbers['number1'] ?>" />
<input type="hidden" name="number2" value="<?php echo $numbers['number2'] ?>" />
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($numbers['number1'] + $numbers['number2']);
?>
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'];
okay so i have made the following script to sort some data i have in this format
0|1|2
heres my script (this now works)
<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="pwd" value="1" size="1" />
<input type="submit" value=" send " name="btn-submit" />
</div>
</form>
</body>
</html>
<?php
if (isset($_POST['mp'], $_POST['delim'], $_POST['btn-submit'], $_POST['mail'], $_POST['pwd'])) {
$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"]];
$password = $mp[$_POST["pwd"]];
// Prepare a reusable string
$result_string = "" . $email . " : " . $password . "";
// Add result to the array
$result_data[] = $result_string;
}
// Store of results complete. We now display them.
if (count($result_data)) {
echo "<ul
list-style-type: none;
>";
foreach ($result_data as $result_data_entry) {
echo "<li list-style: none;>" . $result_data_entry . "</li>";
}
echo "</ul>";
}
}
?>
i would now like to save the results into a text file also
any help would be brilliant .
i am writing this extra line as my post has way way too much code .
You aren't using the data1 and data2 fields anywhere in your PHP.
I think instead of this:
list($email, $password) = $mp;
you need
$email = $mp[$_POST["mail"]];
$password = $mp[$_POST["pwd"]];
It's also worth noting that the values supplied in those two fields will be zero based.
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>';
}