PHP - Loading Values From $_post[] In An Array?
Well I have a script file that loads lots of info from a form using $_POST[] method, which is quite tedious:
Code: [Select] $act = $_POST["act"]; $page = $_POST["page"]; $id = $_POST["id"]; $category = $_POST["category"]; $itemname = $_POST["itemname"]; $description = $_POST["description"]; $imageurl = $_POST["imageurl"]; $existingimageurl = $_POST["existingimageurl"]; $function = $_POST["function"]; $target = $_POST["target"]; $value = $_POST["value"]; $shop = $_POST["shop"]; $price = $_POST["price"]; $tradable = $_POST["tradable"]; $consumable = $_POST["consumable"]; I was wondering if there is a way to write one or two simple lines of code to load all variables stored in superglobal array $_POST[] efficiently. The point is to store all values within $_POST[] to an array called $item[], what I was thinking about is: Code: [Select] foreach($_POST = $key as $val){ $item['{$key}'] = $val; } Seems that its not gonna work, so I wonder if anyone of you have ideas on how I am able to simplify my code with 10-20 lines of $_POST[] to just 2-3 lines. Please do lemme know if this is possible, thanks. Similar TutorialsGreetings folks, I've tried searching on this but couldn't find anything which I think is odd since I'm sure I'm not the first person with this question... I have a project that is basically a big (really big) form. When the user hits submit they get a CSV file. My client will then import the CSV into their database. The CSV is made up of two lines, the first being field names and the second line being the field values. Both the names and values are pulled from $_POST. The CSV looks like this: "Name","Age","Favorite Color" "Bob","12","Purple" The problem I having is if the user skips a question, let's say Age for this example, the CSV will look like this: "Name","Age","Favorite Color" "Bob","Purple" The clients database then freaks when it sees the value for Age is Purple. So my question is, how does one handle null values from from $_POST to avoid such an issue? Thank you! Thank you! Thank you! I have a table that I must be populated based on a check box selection. The maximum selection is 5, however, there are more then 10 choices. For example: Here is a choice of 10 different (checkbox) options that the user can select from: 1. Coke 2. Root Beer 3. Pepsi 4. Dr. Pepper 5. Sprite 6. 7up 7. Cream Soda 8. Club Soda 9. Water 10. Milk I've already managed to limit the users selection to only 5. My issue is determining which of the checkboxese were actually selected so that I can correctly place them in my database. The first thing to do is create variables from all the checkboxes: $coke = $_POST['coke']; $rootbeer = $_POST['rootbeer']; $pepsi = $_POST['pepsi']; $drpepper = $_POST['drpepper']; etc. etc. Now that I have all 10 selections as variables from a submitted form how can I determine which ones are empty (or which ones have values) so that I can then save them to the database respectively? Hi all I am populating a List menu with values from Mysql table. The values are populated correctly. Once a selection is made and upon submit when I display POST submit values using print_r($_POST) the result is correct only when the value do not contain spaces. For example if the list selection is "One" the $_POST[sel] value is correct. But if the selection is "Two Three" it is only first part before space. How do I get it to give complete value as in the list. Thanks and Regards SKN Hi I would like to generate list of $_POST values using records from database: example: Code: [Select] $amount1=$_POST['amount1']; $amount2=$_POST['amount2']; $amount4=$_POST['amount4']; $amount7=$_POST['amount7']; where number next to amount word is simply id value in the database. $_POST values i get from the form erlier. I tried following way and some other ways too but doesnt seems to work (and im not even sure if possible). Code: [Select] $result2 = mysql_query("SELECT * FROM list") or die(mysql_error()); while($row2=mysql_fetch_array($result2)) { $amo = "amount"; $amount = $row2['id']; $amo1 = $amo.$amount; $amo1 = $_POST['$amo1']; echo "($amo1)"; } Hope is clear what i want to do! Thank you very much in advance. I'm trying firstly to output hidden fields based on the values of the $_POST variable. Then after that I want to output the values again in a unordered list. I'm using the following code: while (list($key, $val) = each($_POST)) { if ($key != 'Submit') { echo '<input type="hidden" name="' . $key . '" value="' . $val . '" />'; } } echo '<ul>'; while (list($key, $val) = each($_POST)) { if ($key != 'Submit') { echo '<li><strong>' . $key . '</strong>: <span class="highlight-219ddb">' . $val . '</span></li>'; } } echo '</ul>'; However the <ul> appears to be blank? Can I only use the list() function once on a particular variable? Hello everyone, I'm trying to code a section of the site which is like an advertising website (i.e, gumtree) where people can post an Ad on the site and have any responses sent to them via email. The thing is that I have a form where they can enter their details and need to POST the information to the following page which is a preview page of their Ad. I have the form action returning to the same page for validation but I need the $_POST vaules to populate the following page without sending information to the database (in an effort to keep the database clean should they decide not to publish their Ad. There are 3 pages in total CREATE AD > PREVIEW AD (where they can go back and edit if they need to) > Publish AD (where they confirm terms and send to database). I do have session variables already set after login in and a seperate table to hold the details after they have confirmed the Ad & terms. Any ideas would be greatly appreciated. Thanks, L-plate So, i need this. Hi all, I'm looking for some pointers in regards to my form.. How would I firstly trim the $_POST value of the variables that come through via the form (I'm only using one for now)..I know I'm making a right dogs dinner of it. In my head I'm thinking, trim all the posts first before i even assign a variable to it ( i dont know if thats possible), then use an array for when more values start coming through via the form. You know as i make a contact form that requires more data from the user..
<?php require_once '../connection/dbconfig.php'; include_once('../connection/connectionz.php'); //get the values //Get the request method from the $_SERVER $requestType = $_SERVER['REQUEST_METHOD']; //this is what type //echo $requestType ; if($requestType == 'POST') { //now trim all $_POSTS $search_products = trim($_POST['search_products']); // if(empty($search_products)){ echo '<h4>You must type a word to search!</h4>'; }else{ $make = '<h4>No match found!</h4>'; $new_search_products = "%" . $search_products . "%"; $sql = "SELECT * FROM product WHERE name LIKE ?"; //prepared statement $stmt = mysqli_stmt_init($conDB); //prepare prepared statements if(!mysqli_stmt_prepare($stmt,$sql)) { echo "SQL Statement failed"; }else{ //bind parameters to the placeholder mysqli_stmt_bind_param($stmt, "s", $new_search_products ); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); echo'<h2> Search Result</h2>'; echo 'You searched for <strong><em>'. $search_products.'</em></strong>'; while($row = mysqli_fetch_assoc($result)){ echo '<h4> (ID : '.$row['pid']; echo ') Book Title : '.$row['name']; echo '</h4>'; } } } } ;?>
If any one can shed some light on this, or some pointers..that would be very nice... Thanks Darren
Code: [Select] <?php $query = "Select * from users where username = '$user' "; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); $test = $_POST['test1']; $ques = "Select * from questions where testname = '$test' "; $qres = mysql_query($ques) or die(mysql_error()); $qdetails = mysql_fetch_array($qres); $id = $qdetails['id']; $testname = $qdetails['testname']; $ans = "Select * from answers where qid = $id"; $ares = mysql_query($ans) or die(mysql_error()); if($qdetails) { ?> <div class="padding"> <form name="answerform" action="answer.php" method="POST"> <h3> </h3> <input name="test2" id="test2" type="text" value="<?php echo $qdetails['testname'];?>" /><h3><?php echo $qdetails['text'];?></h3> <input name="test3" id="test3" type="text" value="<?php echo $qdetails['testseries'];?>" /><h3><?php echo $qdetails['text'];?></h3> <br /> <br /> <br /> <?php while($opdetails = mysql_fetch_assoc($ares)) { ?> <input class="text" id="opt2" name="correctans" type="radio" value="<?php echo $opdetails['text']; ?>" /><br /><?php echo $opdetails['text']; ?> <?php }?> <div class="two-fields clearfix". <p class="confirm"> </p> </div> <input type="submit" value="SUBMIT ANSWER" /> </form> </div> </div> </div> <?php } this is the code for the program where i am creating the fields i want to fetch the data from the input fields with the name test 2 and test 3 in answer.php Code: [Select] $query = "Select score from users where username = '$user' "; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); $user_score = $row['score']; print_r($_REQUEST); $tester = $_POST['test3']; $test = $_POST['test2']; print_r($_POST); var_dump($_POST); $ques = "Select * from questions where testname = '$test' And testseries = '$tester' "; $result = mysql_query($ques) or die(mysql_error()); $row = mysql_fetch_array($result); $qid = $row['id']; $score = $row['score']; if(isset($_POST['correctans'])) { $answer = $_POST['correctans']; } else { $answer = ''; } $fetch_ans = "Select * from answers where correct = 1 AND qid = $qid"; $result = mysql_query($fetch_ans) or die(mysql_error()); $row = mysql_fetch_array($result); $id = $row['id']; $correctans = $row['text']; if('$correctans' == '$answer') { $user_score += $score; $qid++; $query = "Update users set score = $user_score where username = '$user'"; $res = mysql_query($query) or die(mysql_error()); if($res) { header("Location: answer.php"); } }else { header("Location: answer.php"); } ?> THIS IS answer.php and here i try to post information from test2 and test3 fields but i am not getting any output the output shows blank array() and unidentified index test2 and unidentified index test3 i am not able to figure out the error any help will be highly appreciated and forgive me if i have made any mistakes in posting the question since i am a newbie at PHPFREAKS Is there an easy way to dump out the contents of a $_POST array and display them on my screen? (I'm trying to get more comfortable with what is stored in the $_POST array and how it is structured.) Thanks, TomTees Hi. First timer here. I'm trying to iterate through 5 arrays in a foreach loop. Yep, I know you'll say just use a counter that increments. Sadly, that gives me horrendous results! So, I am using the next & current methods of arrays. Thing is, it gives me bad results ( items that weren't even chosen on the next page ), or it leaves the first couple of items out. I did this : Code: [Select] <?php $ID = array(); $NAME = array(); $PRICE = array(); $QUANT = array(); $checkCount = 0; foreach (array_keys($_POST) as $key) { $$key = $_POST[$key]; $iPos = strpos($key, "ID"); $nPos = strpos($key, "NAME"); $pPos = strpos($key, "PRICE"); $qPos = strpos($key, "QUANT"); $cPos = strpos($key, "CHECK"); if ($iPos !== false) { $ID[] = $$key; } else { //echo "The string ID was not found in the string "; } if ($nPos !== false) { $NAME[] = $$key; } else { //echo "The string NAME was not found in the string "; } if ($pPos !== false) { $PRICE[] = $$key; } else { //echo "The string PRICE was not found in the string "; } if ($qPos !== false) { $QUANT[] = $$key; } else { //echo "The string QUANT was not found in the string "; } if ($cPos !== false) { $checkCount++; if ($checkCount == 1) { echo "Product ID <input type = 'text' name = 'ID" . current($ID) . "' value = '" . current($ID) . "'></br>"; echo "Product Name <input type = 'text' name = 'NAME" . current($NAME) . "' value = '" . current($NAME) . "'></br>"; echo "Product Price <input type = 'text' name = 'PRICE" . current($PRICE) . "' value = '" . current($PRICE) . "'></br>"; echo "Product Quantity <input type = 'text' name = 'QUANT" . current($QUANT) . "' value = '" . current($QUANT) . "'></br>"; } if ($checkCount > 1) { echo "Product ID <input type = 'text' name = 'ID" . next($ID) . "' value = '" . next($ID) . "'></br>"; echo "Product Name <input type = 'text' name = 'NAME" . next($NAME) . "' value = '" . next($NAME) . "'></br>"; echo "Product Price <input type = 'text' name = 'PRICE" . next($PRICE) . "' value = '" . next($PRICE) . "'></br>"; echo "Product Quantity <input type = 'text' name = 'QUANT" . next($QUANT) . "' value = '" . next($QUANT) . "'></br>"; } } else { //echo "The string CHECK was not found in the string "; } } ?> You will see I did all the processing under the CHECK if statement, the reason for this is, it is easier to identify the items ordered because they have been checked on the previous page. How do I post a for loop array in the next page. I am printing Question ID and choices for these and would like the for loop to print Question ID and choice that are checked for these. If the choice is ticked then it shoul return a 1 if the choice is empty then it should print 0. and I would like to insert this to a new table. How would I do this. Code: [Select] $intNum = 1; $intnumber = 1; while( $info = mysql_fetch_array( $sqll )){ echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> "; echo " $intNum, {$info['Que_Question']} <br />\n"; $intNum++; ?> <br> <?PHP for ($i =1; $i < 5; $i++) { echo "<input type=\"checkbox\" name=\"choice[{$intnumber}]\" />{$info['Que_Choice'.$i]}"; $intnumber++; } this is my form that print the question and it works fine. Code: [Select] <?PHP foreach ($_POST["Que_ID"] as $question) { $_POST["choice"] = $choice; echo "Question id: ".$question. $choice"<br />"; This is the post code but I know I need to do so much more but not sure how? Ayone help me or direct me to a source or tutorial or something. How do I assign the entire $_POST array to a varible? This code isn't working... <?php echo 'FormHandler'; class FormHandler{ private $formValues = array(); $this->formValues = $_POST; } ?> TomTees I am trying to post while loop array. But I dont know how to do this and I used foreach and it works for each array but doesnt loop this. My table has Que_ID, Question, choice1, choice2, choice3 and choice 4. I would like to do something like.. For each Que_id (choice1, choice2, choice3 , choice4), this should be looped for each que_id so far I have Code: [Select] $counter = 1; while( $info = mysql_fetch_array( $sqll )) //)) { echo "{$info['Que_ID']} <br />\n"; echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> "; echo "{$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice[$counter]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; $counter++; echo "<input type=\"checkbox\" name=\"choice[$counter]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; $counter++; echo "<input type=\"checkbox\" name=\"choice[$counter]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; $counter++; echo "<input type=\"checkbox\" name=\"choice[$counter]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; $counter++; How would go on to posting these while loop and display Que_ID and all the choices that are ticked for this que_ID and if its not ticked then its 0. I have a form that is populating fields based on a database query. I am then trying to update the database based on any changes made in the form and passed through a POST action. The problem I am having is with the block where I declare short variable names for the $_POST variables that will be passed when the user hits the submit button. For each variable declaration I get an error message like this: Quote Encountered error: 8 in mbr_profile_updt_form.php, line 130: Undefined index: email I have tried testing the $_POST variable using the isset function but it apparently is set but empty. What would be a good way to test to see if the user has pressed the submit button before I go into the processing block that is intended to handle those values from the form? Here is where my code is at right now: Code: [Select] if (!isset($_POST)) { throw new Exception('You have not filled the form out completely - please go back' .' and try again.'); } else { //filled in so create short variable names $email=$_POST['email']; $first_name=$_POST['first_name']; $last_name=$_POST['last_name']; So ive created a forum that I got handling several different approvals. (it is used to approve or decline all submitted notes) it works fine and it even submits fine im at the part know where i process the information and do something with it, below is my form: <form action="course_proccess.php?action=approve" method="post"> <table align="left" border="1" cellspacing="0" cellpadding="3"> <tr> <td><b>Author</b></td> <td><b>File name</b></td> <td><b>Download</b></td> <td><b>Suggested catagory</b></td> <td><b>Select Catagory</b></td> <td>Approve</td> </tr> <td><input type="text " disabled="disabled" name="tr1_author" value="MikeH" </td> <td><input typee="text " disabled="disabled" name="tr1_name" value="A lot of my skills set back"</td> <td><a href= /uzEr%20Upl0ds/cache.zip > Download</a></td> <td>1001 Laws - Polceing </td> <td><select name="tr1_scatagory"> <option value="1"> Intro To Law </option> <option value="2"> 1001 Laws - Police Foundations </option> </select></td> <td><select name = "tr1_approve"> <option value = "1"> Yes</option> <option value = "2"> no</option> </select></td> </tr> <td><input type="text " disabled="disabled" name="tr2_author" value="MikeH" </td> <td><input typee="text " disabled="disabled" name="tr2_name" value="MM"</td> <td><a href= /uzEr%20Upl0ds/Michael Heintzman.doc > Download</a></td> <td>1001 Laws - Polceing </td> <td><select name="tr2_scatagory"> <option value="1"> Intro To Law </option> <option value="2"> 1001 Laws - Police Foundations </option> </select></td> <td><select name = "tr2_approve"> <option value = "1"> Yes</option> <option value = "2"> no</option> </select></td> </tr> <input type="hidden" name="rowCount" value="2"> <tr> <td><input type="submit" name="sendData" value="send"/></td> </tr> </table> </form> so what I do in my process file is I have a for loop for all of the "TR's" in the table and if it is approved I have an if statment and then I want to add db stuff my problem is my $_POST's are returning blank and I have no idea why. Can someone please assit me with this? (p.s i have debugged and it does get into the if approve == 1 statment. if(isset($_GET['action'])){ if ($_GET['action'] == "approve"){ if(isset($_POST['sendData'])) { $dataArray = array(); $row_count = $_POST['rowCount']; for($i=1;$i<$row_count;$i++) { $approven = "tr".$i."_approve"; $approve = $_POST[$approven]; if($approve == 1) { //Move the info over to "approved files" and delete it out of newnotes $author = "tr".$i."_author"; $location = "tr".$i."_location"; $catagory ="tr".$i."_scatagory"; $filename = "tr".$i."_name"; $submitedby = $_POST['author']; die($submitedby); $dataArray[]=array('author'=>$author,'fileName'=>$fileName,'Catagory'=>$catagory,'location'=>$location); var_dump($dataArray); }else{ //Email user saying they did not recive credits AND delete the entry } } if(!empty($dataArray)) { //now do what you want with the dataArray... var_dumb($dataArray); } } } I need to email the form http://keegenk.com/providenceltddesign/ to myself. Right now, the email is sending and the confirmation page comes up, but the email does not have any of the data that should be collected by the form. I'll attach the php file that I'm using to capture the variable and send it to the email address. Any and all help is appreciated. Thanks a bunch Hi all, I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go! I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays. I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too. For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence? Thanks in advance, as above, I've been searching really hard for the answer and believe i'm over my head! Output of print_r($updates); CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #35 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => ) Output of print_r($updates->result_array()); Array ( [0] => Array ( [problem_update_id] => 1 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Some details about a paricular issue [problem_update_active] => 1 ) [1] => Array ( [problem_update_id] => 4 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Another update about the problem with an ID of 3 [problem_update_active] => 1 ) [2] => Array ( [problem_update_id] => 5 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of four [problem_update_active] => 1 ) [3] => Array ( [problem_update_id] => 6 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of 6 [problem_update_active] => 1 ) [4] => Array ( [problem_update_id] => 7 [problem_id] => 3 [problem_update_date] => 2010-10-12 [problem_update_text] => Some new update about the problem with the ID of 3 [problem_update_active] => 1 ) ) Hi, i'm new here and hope i've finally found a forum where i can find the perfect solution for my questions.
Well since a year now i started to work with PHP and still now and then there are some things where i seize on. My question is on of those situations.
I have the following code:
if (isset($_POST)){ foreach ($_POST as $key => $value){ if (!empty($value)){ $update_rma_detail_stmt = $dbh->prepare("UPDATE rma_detail SET $key = ? WHERE rd_rma_nr = ?"); $update_rma_detail_stmt->bindParam(1, $value); $update_rma_detail_stmt->bindParam(2, $getadmrmaid); $update_rma_detail_stmt->execute(); } } }All i want is when there is an empty (not filled in) input field because it is not mandatory the foreach still continuous to the end. When i use if (!empty($value)){It still outputs these error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ' ' However, when i dont use i got an other error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '' It is however so that the datetime is the very first empty field it come across when looping. I think my if (!empty($value)){ is doing the right thing but still gives error's on integers. Can it be? I allready tried to use trim() or array_filter() but because i previously never used it i dont know if i am doing it the right way so any help is welkom! Thanks! Edited by Raz3rt, 20 May 2014 - 02:56 AM. Hi All, There is a very interesting solved topic here (http://www.phpfreaks.com/forums/index.php?topic=304966.0) about how to access elements in embedded _POST array. But before accessing an element, it might be appropriate to check whether the array it is supposed to be in exists. According to the solution given in the topic indicated above, Code: [Select] $_POST['matType'] is an example of embedded array . Is there a way to check if this array exists? Thanks in advance for your help. |