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'];
Related
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 do a form, inside I would like to do a loop for to show the same field. Each field will have different value and I would like to use sessions to take all the value.
Here is my code:
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "Numero ";
echo $i;
?>
<input type="text" name="number2" id="number2"/>
<?php
}
?>
</form>
<?php
echo $_POST['number2'];
$my_array=array($_POST['number2']);
$_SESSION['countnumb']=$my_array;
in another page:
foreach($_SESSION['countnumb'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
I can't register any number. How can I do this? thanks
Basics First - ids should be unique in a webpage.
Declaring <input type="text" name="number2" id="number2"/> in a loop is wrong.
For creating multiple input using loop try like this-
echo "<input type='text' name='number[$i]' id='number{$i}' />";
<input type="text" name="number[2]" id="number2"/>
would make $_POST['number'] an array which you can loop though server side, This is described here http://www.php.net/manual/en/faq.html.php#faq.html.arrays
foreach ($_POST['number'] as $number){
echo $number;
}
for example
this would make your code
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "<input type="text" name="number[$i]" id="number{$i}"/> ";
?>
<?php
}
?>
</form>
<?php
print_r( $_POST['number'] );
$_SESSION['countnumb']= $_POST['number'];
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'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've problem with multiple form at one page. At page index I include 4 forms
include('./poll_1a.php');
include('./poll_2a.php');
include('./poll_3a.php');
include('./poll_4a.php');
The form code at every poll page is the same. I include some unique markers ($poll_code) for every scripts but the effect is when I use one form - the sending variable are received in the others. But I would like to work each form individually.
The variable $poll_code is unique for every script -> 1 for poll_1, 2 for poll_2 etc.
The same situation is with $cookie_name
$cookie_name = "poll_cookie_".$poll_code;
than, as I see, cookies have different names.
$poll_code = "1"; // or 2, 3, 4
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="<?php echo $poll_code; ?>">
<input type="hidden" name="poll_cookie_<?php echo $poll_code; ?>" value="<?php echo $poll_code; ?>">
<table>
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><tr><td style="\text-allign: left;\"><input type="radio" name="vote_<?php echo $poll_code; ?>" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?></td></tr><?php
}
echo "</table>";
echo "<br>";
if ($_COOKIE["$cookie_name"] == $poll_code ) {
echo "<br> nie można głosować ponownie ...";
} else {
?>
<p><input type="submit" name="submit_<?php echo $poll_code; ?>" value="głosuj !" onClick="this.disabled = 'true';"></p>
<?php
}
?>
</form>
</p>
Q: how to make this forms to work individually at one page?
//------------------- EDIT
the receiving part of the script
$votes = file($file);
$total = 0;
$totale = 0;
$poll_cookie = 0;
if (isset($_POST["vote_$poll_code"]) && isset($_POST["poll_cookie_$poll_code"])) {
$vote = $_POST["vote_$poll_code"];
$poll_cookie = $_POST["poll_cookie_$poll_code"];
}
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
//write votes
$handle = fopen($file,"w");
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
fclose($handle);
Of course, the $file have the unique declaration too (at top of the script, under the $poll_code declaration).
$file = "poll_".$poll_code.".txt";
I think the issue might be that <?php echo $poll_code; ?> is outside the loop so maybe that it's always using the same value assigned to it, maybe put it inside the loop