PHP - How To Get Values From Textboxes Generated Through A Loop.
Hi there,
I am working on a PHP form and running a loop to generate textboxes at runtime. This code output the textboxes: foreach ($MyTest as $tst) { echo "<input name='testName' type='text' size='12' maxlength='5' />"; } I am able to display (or output) 4 or sometimes 6 textboxes depending on the variable $MyTest. Now after I enter the values in these texboxes. Lets say, it outputs 3 textboxes, I woud like to display the values like: In Textbox 1 you entered: In Textbox 2 you entered: In Textbox 3 you entered: How can I get (or fetch) values from the textboxes generated through the loop. Please reply. All comments and feedbacks are always welcomed! Thank you Similar TutorialsHi all, I have a situation where I need to remember what check boxes where checked over pagination, I have managed to do this via the use of this: http://jamesfunk.com/wordpress/?p=65 The problem is that as the code stood: Code: [Select] <input type="checkbox" name="compare" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/> It was treating one ticked checkbox as them all because they all have the same name and are in the a while loop! I countered this by changing the code to: Code: [Select] <input type="checkbox" name="compare<?php echo $list['jobseeker_id']?>" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/> Which effectively now makes the checkbox name unique... i.e. compare{id}. The problem with this is that I can now no longer process it. This is my processing code: $jobseekers = $_POST['compare']; $i = 0; for($i; $i<count($jobseekers); $i++){ $query = "SELECT * FROM jobseekers WHERE jobseeker_id = '$jobseekers[$i]'"; $result = mysql_query($query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { // Spit out the required data } } As you can see I am trying to get the data from $_POST['compare'], which will obviously now be blank as I have had to make the name unique.... the trouble is I'm not sure how to actually process this. Can anyone help me out here? any help or advice would be greatly appreciated! many thanks, Greens85 hello, i'm actually doing a javascript here to create dynamic textboxes when a button is clicked. now my problem is, how to get all the values from these textboxes and save these values altogether in a column on the table.. i know few php coding methods like $_POST in handling values from txtboxes and saving it to database but it doesn't include working with javascript and dynamic textboxes.. and i'm actually doubtful if this is possible..so any help from you guys i would deeply appreciate. heres my code: Code: [Select] <html> <head> <script type="text/JavaScript"> function AddTextBox() { document.getElementById('container').innerHTML+='<input type="text" name="block"><br>'; } </script> </head> <body> <input type="text" name="block"> <div id="container"></div> <br> <button onclick="AddTextBox();">Add another textbox</button> </body> </html> <!DOCTYPE html> <html> <head> <script> $(document).ready(function() { if($("#getPIN").click(function(){ <?php $char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890'; $generated_password = substr(str_shuffle($char), 0, 10); ?> var genNumber; $("#txtpin").val() = genNumber; return false; })); }); </script> </head> <body> <input type = "text" name = "txtpin" id = "txtpin" value = ""> <input type = "button" name = "getPIN" id = "getPIN" value = "getPIN"> </body> </html> Hey you guys need your help... Basically what im trying to do is when the user clicks the getPIN button, the value generated using php is assigned to the txtpin textfield without refreshing the page.... thanks alot guys.... Hi, I have a database with some rows. in each row there is a number such as: 0.0023 or 0.0135... I have the following code to loop though them and add the numbers up but it always returns 0? Code: [Select] $sql="SELECT * FROM tickets WHERE `round` = '$round' AND `confirmed` = 'yes' ORDER BY id ASC"; $result=mysql_query($sql); $row=mysql_fetch_array($result); while($row = mysql_fetch_array( $result )) { $balance = $balance + $row['amount']; } Is it possible to use for loop to insert values into database using for loop? Or what would be the another way to insert these values into the database if the number of values being entered differs from one time to another? Here is basically what I am trying to do: mysql_query("INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDate', DateTime='$timestamp', for($i=1; $i<=$SetNumber; $i+=1){ for($j=0; $j<7; j+=1){ $NumberName="Number".$i.$v[$j];//Generating name of database field $$NumberName=$Match[0][$j];//Here I would store the $Match value into the fieldname name created above } } Tries='$RunCounter'"); Hi.. I have table which has a data so_month: FromMonth : 5 ToMonth : 7 and I have table working_days: MonthName May Jun Jul MonthNumber 05 06 07 WorkingDays 23 24 23 Now I have function to get the 3 consecutive months from FromMonth to ToMonth , which as you can see from May to Jul Now I have problem in getting the SUM of Working days. here is my code: <?php $sql = "SELECT FromMonth, ToMonth FROM so_month"; $res = mysql_query($sql,$con); $row = mysql_fetch_assoc($res); $FromMonth = $row['FromMonth']; $ToMonth = $row['ToMonth']; function monthNames($from, $to){ $range=array(); for($i=$from; $i<=$to; $i++){ $range[$i]=date('M', mktime(0,0,0,$i)); } return $range; } $month_ = implode("' ', ",monthNames($FromMonth,$ToMonth)); foreach( monthNames($FromMonth, $ToMonth) as $month){ $sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName"; $res = mysql_query($sql, $con); while($row = mysql_fetch_array($res)){ $WorkingDays = $row['WorkingDays']; } echo $WorkingDays; } ?> the output of this code is: 232423 and when I change this line: $WorkingDays = $row['WorkingDays']; to $WorkingDays += $row['WorkingDays']; the output is: 234770 I correct output should be: 70 Any help is highly appreciated. Thank you very much.. Hope someone can help, I am trying to add up all the values of a variable in a PHP while loop and have tried several approaches but none seem to work. Hopefully someone can point out what the correct method is. I've set up some code to pull the pay rate from a MySQL table, and then calculate that person's earnings based on the amount of time they spend on a particular job. That works fine:
However there may be a number of jobs for different people with different rates, so I want to add all of these up and output the total. So for example if I had values of 125.00, 35.50 and 22.75 I want to show 183.25 as the total. So I tried a few things I found on forums on the subject:
That didn't work so I tried another method that someone suggested:
The 2nd method outputted just the last individual value, the 1st method didn't work at all. Does anyone have a working method that can be used to do this? Thanks First of all i create the page to copy the buttons as pixels to paste into a design, it's not for a working webpage. It goes wrong here, <option value="0">Day</option> <?= for(var i=0; i<31; i++){?> <option value="<?=i?>"><?=i?></option> <?= } ?> </select> Code: [Select] <?php ?> <html> <head> <title>Community Development Project</title> </head> <body> <div id="container"> <form id="opties1" name="opties1" method="post" action="something.php"> <div class="forminput"> <input type="button" id="signin" name="signin" value="Sign In" /> <br/> <br/> <input type="button" id="register" name="register" value="Register" /> <br/> <br/> <input type="button" id="uploadimage" name="uploadimage" value="Upload Image" /> <br/> <br/> <input type="button" id="next" name="next" value="Next" /> <br/> <br/> <input type="button" id="addanother" name="addanother" value="Add Another" /> <br/> <br/> <input type="button" id="register" name="register" value="Register" /> <br/> <br/> <input type="button" id="create" name="create" value="Create" /> <br/> <br/> <input type="button" id="addfolder" name="addfolder" value="Add Folder" /> <br/> <br/> <input type="button" id="addimage" name="addimage" value="Add Image" /> <br/> <br/> <input type="button" id="addtask" name="addtask" value="Add Task" /> <br/> <br/> <input type="button" id="cancel" name="cancel" value="Cancel" /> <br/> <br/> <input type="button" id="watch" name="watch" value="Watch" /> <br/> <br/> </div> </form> <form id="opties2" name="opties2" method="post" action="something.php"> <div class="forminput"> <select id="day" name="day"> <option value="0">Day</option> <?= for(var i=0; i<31; i++){?> <option value="<?=i?>"><?=i?></option> <?= } ?> </select> <select id="country" name="Country"> <option value="0">Netherlands</option> <option value="1">Germany</option> <option value="2">Belgium</option> </select> </div> </form> </div> </body> </html> I am trying to get info from a table, add rows in that table, and put the added value back into the table in another field...I want to add field1 and field2 of every row and put the total in the total row. Heres what the echo gives me Susan 14 Mary 18 Bob 13 Sam 21 heres the database mysql> SELECT total_val FROM table; +-----------+ | total_val | +-----------+ | 21 | | 21 | | 21 | | 21 | +-----------+ heres the code.... <?php require("connection.php"); mysql_select_db("database", $connection); echo "<br />"; $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $values = ($row['field1'] + $row['field2']); $sql=mysql_query("UPDATE table SET total = '$values'"); echo $row['user_name'] . " " . $values; echo "<br />"; } ?> also note that if I put a INT in place of $values - the echo changes to match...why?? PLEASE help...I have been working on this for 2 entire days.... Hello, I need to hide products with same title from WooCommerce shop page and i'm trying to achieve that with a custom loop. For example i have 5 products called "Plex" and the only diferrence between them is the SKU field and the color. I don't care which of the same product will be displayed in the shop page, i just want to display only one of all these products. I created a loop which is working and does hide products with same name but i have a problem. If i set posts_per_page=8 it shows less because it counts the hidden products also. $args = array( 'post_type' => 'product', 'posts_per_page' => 8 ); $query = new WP_Query($args); $list = array(); while ($query->have_posts()) : $query->the_post(); if(in_array(get_the_title(), $list)){ continue; } $list[] = get_the_title(); ?> <li><?php wc_get_template_part( 'content', 'product' ); ?></li> <?php endwhile; wp_reset_postdata(); P.S. I don't want to remove these products, i just want to hide them from shop loop! Any ideas what is the problem with the loop? Is there a better way to achieve that? Hi I am new this forum and looking for some much appreciated help with something that has me stumped. I have extracted regions from image names in a directory. example: 'edinburgh_castle_(Edinburgh).jpg' (Extracted Edinburgh from in-between the brackets) So I have a list of Regions, extracted from all the various image names in the directory which I want to populate into a dynamic drop down menu for filtering purposes. Here's my issue... I want there to be only one 'Edinburgh' option appearing in my drop down menu. I don't want duplicates. Here is my code so far. php: [Select] Please see the following code:
$colors = ["red hello","green this","blue lovely","yellow morning"]; foreach ($colors as $towns) { $hello = (explode(" ", $towns)); }If I do print_r($hello[1]); within the loop I get all the values of index [1] in the browser although not sure why there are no spaces between the words: HelloThisLovelyMorning However if I do the same print outside the loop I only get this displaying in the browser: Morning That's no good to me because I want all the values with the index of [1] so I can use them anywhere I want. Any help would be much appreciated.
hi, i’m currently facing a new problem (i managed to use array_unique function successfully but i face this problem)with my php loop. i couldn’t seem to increment in my foreach loop to get the value of an array. $update_id1=Array ( [0] => 398 [1] => 397 [2] => 393 [3] => 391 [8] => ); //$i=0; foreach ($update_id1 as $i => $v) { $ex= explode(",", $v); $unique=array_unique($ex); //f$ketch username from update table in db and inject it to the feed query. $imp_id= implode(',', $ids); //$id1=$ids[$i]; // $x=0; // for($x=0;$x<count(unique);$x++){ echo 'ids:- '.$i; print_r($unique[$i]); echo '<br>'; //} $totalUpdates=$project->totalUpdates($ids[$i],$_SESSION['uname'] ,$unique[$i],$load); //$i++; } i tried every method nothing seems to work can anyone help me in this regard please. Edited December 25, 2019 by narutofanI have a database query set up that returns an array. I then cycle through the rows in a foreach statement that wraps each value in a <td> tag to output it to a table. It works really great, but now I need to access two of the values and add some info to them. The first field returned is an image filename and I want to wrap it in an image tag that also concatenates the image path location to the value. All the images are stored in the same location, so basically I want to take the first value form the array and add the path to where the images are and wrap the whole thing in an <img> tag. The other field I need to modify is the last value in the array which is actually an email address. For that field I want to make them an active email link so I need to wrap an <a>href=mailto: </a> tag around the value. How could I modify this code so I could add the necessary tags to these elements of the array? Here is the code I am running: Code: [Select] $query = "Select member_image as 'Image', member_name as 'Name', city as 'City', phone as 'Phone', email as 'Email' FROM directory"; //connect to database $conn=db_connect(); //call function do_query to run the query and output the table do_query($conn, $querey); The functions called are as follows: Code: [Select] function db_connect() { $conn = new mysqli("localhost", "username", "password", "databasename"); } function do_query($conn, $query); { $result = mysqli_query($conn, $query); WHILE ($row = mysqli_fetch_assoc($result)) { If (!$heading) //only do if header row hasn't been output yet { $heading = TRUE; //so we only do it once echo "<table>\n<tr<>\n"; foreach ($row as $key => $val) { echo "<th>$key</th>"; } echo "</tr>\n"; } echo "<tr>"; foreach ($row as $val) { echo "<td> $val </td>"; } echo "</tr>\n"; } //close the while echo "</table>\n"; } Hi friends. I want to php code on how i can modify my retrieved database values from a particular table before echoing out. Please could you help me correct the code. Or if there is any better way of editing loop datas before printing out, please kindly drop the code for me. Thanks <?php please if there is any better way of editing loop values from mysql database before printing out, please kindly drop the code for me. Thanks I have my data array which i converted to json by splitting the keys using for each loop. Data:
$data = Array ( [table6-ios-camt] => [table6-ios-iamt] => [table6-ios-samt] => [table6-ios-type] => [table6-isd-camt] => 0 [table6-isd-iamt] => 0 [table6-isd-samt] => 0 [table6-isd-type] => [table6-ios-csamt] => [table6-isd-csamt] => 0 [table6-other-camt] => [table6-other-iamt] => [table6-other-samt] => [table6-other-type] => [table6-tran1-camt] => 0 [table6-tran1-iamt] => [table6-tran1-samt] => 0 [table6-tran1-type] => [table6-tran2-camt] => 0 [table6-tran2-iamt] => [table6-tran2-samt] => 0 [table6-tran2-type] => [table6-itc_3b-csamt] => 0 [table6-dump_iog-camt] => [table6-dump_iog-iamt] => [table6-dump_iog-samt] => [table6-dump_iog-type] => [table6-dump_oth-camt] => 4386.09 [table6-dump_oth-iamt] => [table6-dump_oth-samt] => 4386.09 [table6-dump_oth-type] => [table6-itc_clmd-camt] => [table6-itc_clmd-iamt] => [table6-itc_clmd-samt] => [table6-itc_clmd-type] => [table6-total_10-camt] => 4386.09 [table6-total_10-iamt] => 0 [table6-total_10-samt] => 4386.09 [table6-total_10-type] => [table6-total_11-type] => [table6-total_8-csamt] => 0 [table6-total_9-csamt] => 0 [table6-dump_iog-csamt] => [table6-dump_isrc-camt] => [table6-dump_isrc-iamt] => [table6-dump_isrc-samt] => [table6-dump_isrc-type] => [table6-dump_oth-csamt] => [table6-iog_goods-camt] => [table6-iog_goods-iamt] => [table6-iog_goods-samt] => [table6-iog_goods-type] => Capital Goods [table6-itc_clmd-csamt] => [table6-total_10-csamt] => 0 [table6-dump_isrc-csamt] => [table6-iog_goods-csamt] => [table6-iog_inputs-camt] => [table6-iog_inputs-iamt] => [table6-iog_inputs-samt] => [table6-iog_inputs-type] => Input Services [table6-iog_inputs-csamt] => [table6-iog_services-camt] => [table6-iog_services-iamt] => [table6-iog_services-samt] => [table6-iog_services-type] => Input Services [table6-supp_non_rchrg_goods-camt] => [table6-supp_non_rchrg_goods-iamt] => [table6-supp_non_rchrg_goods-samt] => [table6-supp_non_rchrg_goods-type] => Capital Goods [table6-supp_rchrg_reg_goods-camt] => [table6-supp_rchrg_reg_goods-iamt] => [table6-supp_rchrg_reg_goods-samt] => [table6-supp_rchrg_reg_goods-type] => Capital Goods [table6-supp_non_rchrg_goods-csamt] => [table6-supp_non_rchrg_inputs-camt] => 4386.09 [table6-supp_non_rchrg_inputs-iamt] => [table6-supp_non_rchrg_inputs-samt] => 4386.09 [table6-supp_non_rchrg_inputs-type] => Inputs [table6-supp_rchrg_reg_goods-csamt] => [table6-supp_rchrg_reg_inputs-camt] => [table6-supp_rchrg_reg_inputs-iamt] => [table6-supp_rchrg_reg_inputs-samt] => [table6-supp_rchrg_reg_inputs-type] => Input Services [table6-supp_non_rchrg_inputs-csamt] => [table6-supp_rchrg_reg_inputs-csamt] => [table6-supp_rchrg_unreg_goods-camt] => [table6-supp_rchrg_unreg_goods-iamt] => [table6-supp_rchrg_unreg_goods-samt] => [table6-supp_rchrg_unreg_goods-type] => Capital Goods [table6-supp_non_rchrg_services-camt] => [table6-supp_non_rchrg_services-iamt] => [table6-supp_non_rchrg_services-samt] => [table6-supp_non_rchrg_services-type] => Input Services [table6-supp_rchrg_reg_services-camt] => [table6-supp_rchrg_reg_services-iamt] => [table6-supp_rchrg_reg_services-samt] => [table6-supp_rchrg_reg_services-type] => Input Services [table6-supp_rchrg_unreg_goods-csamt] => [table6-supp_rchrg_unreg_inputs-camt] => [table6-supp_rchrg_unreg_inputs-iamt] => [table6-supp_rchrg_unreg_inputs-samt] => [table6-supp_rchrg_unreg_inputs-type] => Input Services [table6-supp_non_rchrg_services-csamt] => [table6-supp_rchrg_reg_services-csamt] => [table6-supp_rchrg_unreg_inputs-csamt] => [table6-supp_rchrg_unreg_services-camt] => [table6-supp_rchrg_unreg_services-iamt] => [table6-supp_rchrg_unreg_services-samt] => [table6-supp_rchrg_unreg_services-type] => Input Services [table6-supp_rchrg_unreg_services-csamt] => ); json: { "table6":{ "ios":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "isd":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "other":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran1":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran2":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "itc":{ "camt":0, "iamt":550, "samt":230, "csamt":680 }, "iog":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_non_rchrg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":4386.09, "samt":4386.09, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_reg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_unreg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ] } }
Keeping in mind the following conditions: 1.If all the keys in the objects are 0 then the entire object will be removed (easy part). 2.If a key in the object has any value other than 0 , the object should not be removed and the key with "0" value should also be there.i've tried splitting value and using conditions to check for 0 or null values but the problem with that is it removes all keys that have 0 values thus failing condition 2 for the code. $vars = $data; $count = ""; $finalArray = array(); foreach ($vars as $keys => $values) { foreach ($values as $k => $v) { foreach ($v as $ke => $ve) { if ($keys == "table6") { $count = (array)$v; foreach ($count as $ck => $cv) { if ($cv != "0") { $flag = true; break; } } if($flag == false) { array_push($finalArray, $count); } } } } print_r($finalArray); die(); I expeced the output to contain the objects with key value pairs that does not contain 0 value but everything prints here. Hi everyone. I have a combo box which lists usernames and onchange, the username value is passed to a textbox. however, I have 3 textboxes i need to populate based on the selection of the combobox: username. department and email. i have the username going into a textbox, but i'm not sure how to pass department and email into two other textboxes. I'd appreciate any help you could provide. Thanks. Code: [Select] <script> function CBtoTB() {document.getElementById("username").value=document.getElementById("usernameselect").value} </script> <?php $result=mysql_query("select Username, EMail, Department from users"); $options=""; while ($row=mysql_fetch_array($result)) { $username=$row["Username"]; $options.="<OPTION VALUE=\"$username\">".$username.'</option>'; } ?> <select name="usernameselect" id="usernameselect" onchange="CBtoTB()"> <option value="">< select user ><?php echo $options ?></option> </select> <input name="username" type="text" id="username" value="<?php echo $username ?>" size="25" readonly="readonly" /> |