PHP - How To Pass Array From Html To Jquery ?
Hi,
Similar TutorialsHi there, I am working on a php web form. I have a variable named $isFruit having value of 1 When I do a: print_r($isFruit) It gives me value as 1, which is good. Now I need to pass this php variable into a jquery function: $('document').ready(function() { //I need to get the value of $isFruit variable here } How can pass the php variable into this function. Kindly reply. All comments and feedbacks are always welcomed. Thank you! Hi I am looking to be able to pass an array to mysql_fetch_row() not sure where i am going wrong. works fine like this. Code: [Select] mysql_select_db($DB_NAME); $new = mysql_query("SELECT * FROM {$DB_TABLE}"); if (!$new) { echo 'MySQL Error: ' . mysql_error(); exit; } $row = mysql_fetch_array($new); while ($row = mysql_fetch_array($new)) { echo $row['username'] . $DELIMITER . $row['firstname'] . $DELIMITER . $row['lastname'] . $DELIMITER . $row['password'] . $DELIMITER. "example@gmail.com" . $DELIMITER . "author"; echo "<br />"; } But really need too be able to do it this way. Code: [Select] mysql_select_db($DB_NAME); $new = mysql_query("SELECT * FROM {$DB_TABLE}"); if (!$new) { echo 'MySQL Error: ' . mysql_error(); exit; } $row = mysql_fetch_array($new); echo "<pre>"; //print_r($row); echo "</pre>"; $sam = array('username','firstname','lastname'); while ($row = mysql_fetch_row($new)) { echo $row[$sam] . $DELIMITER; echo "<br />"; } So it will just loop through the array so i can keep adding to it by just adding an extra parameter to the array. Any Help Stuck???? I have a simple ajax that passes data to a php script using jquery serialize. This is working good but where I am getting stuck is trying to pass json array into mysql. The json is outputting the correct data, but when I insert into db, it just says Array in the field. How can I convert(if that is the right term) json array to use in mysql. In the code I have posted, $box is an array from jquery which I normally use foreach to loop through array. What I need to do is convert $box after is has been encoded. Thanks Code: [Select] $box = $_POST['BRVbrtrv_boxnumber']; $list = array("activity" => $activity, "mobile" => $mobile, "company" => $company, "authorised" => $authorised, "service" => $service, "department" => $department, "address" => $address, "boxcount" => $boxcount, "box" => $box); $c = json_encode($list); echo $c; I am working on search result page where all the thumbnail will display and its working fine. but what my boss wants is when user click on any thumbnail a new window will open with the product image and next and prev button so that user will navigate search result through new window so how i can pass all the ID to new window so that this funtionality can be done thanks in advance.. Hi, I'm trying to pass values from an array into a form. As a newbie, I've came up with the code underneath, but I want to pinpoint all the first values toward 1 selection list in the form. Any suggestions how to accomplish this? Thanks in advance. Ruud <html> <head> <title>Test formulier</title> </head> <body> <h2>Test formulier</h2> <form action="test.php" method="post"> <p> <?php $prijzen = array( array("01", "125","150", "A" => true,"B" => true), array("02", "125","150", "A" => true,"B" => true), array("03", "125","150", "A" => true,"B" => false), array("04", "125","150", "A" => true,"B" => true) ); ?> </P> <select name="prijzen[]" id="prijzen" multiple="multiple" size="7" class="none"> <?php while(list($key, $val) = each($prijzen)){ while(list($key2, $val2) = each($val)){ echo '<option value="'.$key2.'">'.$val2.'</option>'.PHP_EOL; } } ?> </select> </body> </html> Hello All, I have used a function to collect a users information from the database. This is then returned as an array. Using this returned array, I wish to pass it into another function. Here is the code: The array is created in the collect() function and needs to be passed to the ipn_data function Code: [Select] /* This script is responsible for handling all subscription payments. It will collect all information, insert data into the relevant databases and then forward the user to PayPal with the relevant transaction ID. On return, the IPN listener will deal with all account changes whilst the user is held in a queue. */ /* ------------------------------------- */ /* DATABASE CONFIGURATION */ /* ------------------------------------- */ include_once "../config/config.php"; /* ------------------------------------- */ /* COLLECT SESSION INFORMATION */ /* ------------------------------------- */ session_start(); $id = "100000002"; // ID of logged in user $next_year = date("Y-m-d", strtotime("+365 day", time())); //Date next year /* ------------------------------------- */ /* SET VARIABLES */ /* ------------------------------------- */ $reason = $_GET["reason"]; /* ----------------------------------------- */ /* COLLECT USERS INFORMATION FROM DATABASE */ /* ----------------------------------------- */ function collect($id) { //Select the subscriber from the database $query = "SELECT * FROM subscribers WHERE id='$id'"; $query = mysql_query($query); //Collect the subscribers first name, last name and email address while($row=mysql_fetch_array($query)) { $fname = $row["fname"]; $lname = $row["lname"]; $email = $row["email"]; } //Collate subscribers information into an array return array('fname' => "$fname", 'lname' => "$lname", 'email' => "$email"); } /* ----------------------------------------- */ /* CREATE TIMESTAMP OF CURRENT TIME */ /* ----------------------------------------- */ function mktimestamp() { //Set date variables $day = date("d"); $month = date("m"); $year = date("Y"); //Make timestamp return mktime(0, 0, 0, $month, $day, $year); } /* ----------------------------------------- */ /* INSERT USERS DATA INTO ipn_data */ /* ----------------------------------------- */ //Create random identifier $ident = rand("1", "10000"); function ipn_data($info, $reason, $timestamp) { //WANT THE ARRAY TO BE PASSED INTO HERE!! //I TRIED PASSING THE $info VARIABLE INTO THE FUNCTION, WHICH IS WHAT I ASSIGNED TO THE COLLECT FUNCTION LATER IN THE SCRIPT } /* ----------------------------------------- */ /* Get ID of inserted ipn_data row */ /* ----------------------------------------- */ function get_id($ident, $timestamp) { //Setup Query to find ID $query = "SELECT * FROM subscribers WHERE timestamp='$timestamp' AND identifier='$ident'"; //Run Query $query = mysql_query($query) or mysql_error(); //Using query find ID while($row=mysql_fetch_array($query)) { $ltj_txn_id = $row["id"]; } return $ltj_txn_id or mysql_error(); } /* ------------------------------------- */ /* SWITCH REASON */ /* ------------------------------------- */ switch($reason) { case "subscribe": break; case "renew": //Collect array from user information array function $info = collect($id); //Get timestamp from our mktimestamp function $timestamp = mktimestamp(); //Insert users data into ipn_data table $run_ipn = ipn_data($reason, $timestamp, $ident); //Collect ID of record just inserted $get_id = get_id($ident, $timestamp); echo "$run_ipn"; break; case "upgrade": break; default: break; } Any ideas? I want the quiz questions a teacher loads to be randomized so that each time the teacher gives it (different class, different day, whatever …) the order of questions will be randomized. I'm loading the data into an array and then shuffling it. Problem solved. BUT … students on their smart devices need to load the same quiz with the same question order.
I can solve this by:
- teacher stores question and answer data and order temporarily in a dB table
- students load that information
- problem solved
But I was hoping to do this without using a table. Is there a way to pass a randomized array from the teacher to the students without going through a dB table? Thanks.
/* Output of the table will display the Suite based on the distinct date. The pass rate percentage calculate based on the Suite1 and app1 and the specific date. For example at Apr 4 2011, I need to Sum the total passcount,failcount,errorcount of Bluetooth, GPSA, EMIReceiver($app1) of XA9 on Real MXE($suite1) , then calculate the passrate percentages. But I fail to get the correct value of the passcount, failcount, errorcount value.Helps! Output of the table suite Date Percentages(pass rate) XA9 on Real MXE Apr 4 2011 9:47AM 99.94% XA9 on Real MXE Apr 5 2011 10:48AM 99.94% XA9 on Real MXE Apr 6 2011 9:49AM 99.95% XA9.5 on Real EXA_B40 Apr 4 2011 7:06AM 99.94% XA9.5 on Real EXA_B40 Apr 5 2011 7:14AM 99.93% XA9.5 on Real EXA_B40 Apr 6 2011 7:29AM 99.93% */ $suite1 --> array value (XA9 on Real MXE, XA9.5 on Real EXA_B40) $app1 --> array value (Bluetooth, GPSA, EMIReceiver) if(is_array($suite_type1)){ foreach($suite_type1 as $suite1) { $sql222="Select Distinct a.StopDTTM from Suite a,Test b where a.SuiteID=b.SuiteID and b.SuiteID=a.SuiteID and a.StopDTTM>='$fromdate' and a.StopDTTM<='$to_date' and a.SuiteFilePath like '%$Program%' and a.SuiteFileName='$suite1'"; $result222=mssql_query($sql222,$connection) or die("Failed Query of".$sql222); while($row222 = mssql_fetch_array($result222)) { $datetotest = $row222[0]; $days_to_add = 1; $tilldate = fnc_date_calc($datetotest,$days_to_add); //Query the result based on the date $sql223="Select Distinct a.SuiteFileName, a.SuiteID,a.StopDTTM from Suite a,Test b where a.SuiteID=b.SuiteID and b.SuiteID=a.SuiteID and a.StopDTTM>='$row222[0]' and a.StopDTTM<='$tilldate' and a.SuiteFilePath like'%$Program%' and a.SuiteFileName='$suite1' order by a.StopDTTM"; $result223=mssql_query($sql223,$connection) or die("Failed Query of".$sql223); while($row223 = mssql_fetch_row($result223)){ foreach($app_type1 as $app1) { $sql3="Select Sum(b.passcount),Sum(b.failcount),Sum(b.errorcount) from Suite a,Test b where a.SuiteID=b.SuiteID and b.SuiteID=a.SuiteID and a.StopDTTM>='$row222[0]' and a.StopDTTM<='$tilldate' and a.SuiteFilePath like '%$program%' and a.SuiteFileName = '$suite1' and b.Product='$app1'"; $result3=mssql_query($sql3,$connection) or die("Failed Query of ". $sql3); $row3 = mssql_fetch_row($result3); $total_passcount+=$row3[0]; $total_failcount+=$row3[1]; $total_errorcount+=$row3[2]; } $overall_total=$total_passcount+$total_failcount+$total_errorcount; echo "<tr>"; echo "<td><a href='detailsreport.php?suiteID=".$row223[1]."&suitetype=".$row223[0]."&fromdate=".$fromdate."&to_date=".$to_date."&date=".$row222[0]."&tilldate=".$tilldate."&SuiteFilePath=".$Fprogram."'>" . $row223[0] . "</a></td>"; //Suite File Name echo "<td>" . $row223[2] . "</td>"; //Pass percentage if($total_passcount>0){ echo "<td bgcolor=00FF33>" . number_format((($total_passcount/$overall_total)*100),2)."%</td>"; } else{ echo "<td bgcolor=00FF33>0%</td>"; } } } } echo "</table>"; } I am simply trying to insert a value generated from an array in a while loop, but it seems the value is not global and I can't pass it as I like. I did some research on this, but could not find an answer that solved my issue... Here is my select box code which is working perfect: <select name="city"> <?php $sql = "SELECT id, city_name FROM cities ". "ORDER BY city_name"; $results_set = (mysqli_query($cxn, $sql)) or die("Was not able to produce the result set!"); while($row = mysqli_fetch_array($results_set)) { echo "<option value=$row[id]>$row[city_name]</option>"; } ?> </select> Are any variables defined in a while loop global to the while loop only? Here is my SQL which you can see my $row[id] being passed thru field city_id... The value is being generated as supposed to based on value like: value="1", value="2" etc.. for the select options for each city name. So the values are there... But I CANNOT get that numerical id to pass to the database when submitting my form. Any ideas for a workaround to get this value passing as normal? if (isset($_POST['addPosting'])) { $query = "INSERT INTO Postings (id, city_id, title, description) VALUES ('','$row[id]','$_POST[title]','$_POST[description]')"; In my view file, I instantiate User class and call two methods that have not been declared or initialized in the class: $user = new Models\User(); $user->setFirstName('John'); $user->setLastName('Merlino'); echo $user->getFirstName() . " " . $user->getLastName(); However, I have two private members only available to instances: protected $first_name; protected $last_name; Because I don't want to manually create privileged getters and setters for each of my private members, I use the call method: public function __call($name,$args){ echo $name . "<br />"; echo $args . "<br />"; } Because my methods were not initialized, the interpreter calls the __call method as a last option resort, and passes the name of the method in local variable $name and if exists the arguments passed into the method in local variable $args. Ok so that makes sense. However, when I echo the values of the two variables, I see a blank array created: setFirstName Array setLastName Array getFirstName Array getLastName Array See everytime I call it, there's an array being created. I don't see the purpose of this. Also how often do you use __call() in your php applications, specifically when using MVC? Thanks for response. Is it possible for a clickable image to pass to a php page? It can lead to a linked web page, so can it be like a submit button on a form and pass to the $_POST in php?
Hello World, I need some tips on how to make a button that does something like the code below. .......echo '<tr><td align="left"><a href="add_cart.php?pid=' . $row1['item_id'] . '">Add to Cart</a></td>........ I've been messing with html form and button type with no success. This code does the job but I prefer a button. Thanks I have a page that contains a List box containing a list of categories taken from a mysql database, 4 iframe elements and 4 submit button (code to follow)
When an item is selected from the List box the onchange event submits the page to jobs.php and loads it into the joblist iframe. Once the iframe is loaded and visible it makes the "Add Job To Category" submit button visible. This all works great
When I click the "Add Job To Category" Submit button it loads Jobnew.php into the jobed iframe this is working but I can not seem to figure out how to pass the selected item from the List box using this submit button
I hope I am clear in my question if not please advise any help is very much appreciated....
Jobtask.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Job Configuration</title> <head> </head> <body> <div style="position:absolute;left:5px;top:0px;width:1345px;height:50px;z-index:0;"> <img src="images/img0001.png" id="Shape1" alt="" style="border-width:0;width:1345px;height:50px;"></div> <span style='position:fixed;left:525px;top:8px;font-family:Arial;font-size:32px;width:275px;'>Job Configuration</span> <form action='index.html' target='' method='post'> <div style="position:absolute;left:205px;top:55px;width:1345px;height:50px;z-index:0;"> <Input type="submit" action="index.html" Target="_top" method='post'Value="Main Menu"> </div> </form> <form style=position:fixed;left:200px;top:82px;> <iframe name='joblist' id='joblist' style=position:fixed;left:200px;top:px;visibility:hidden;z-index:10 src='' height="475" width="1150" scrolling='yes' frameBorder='0' ></iframe> <iframe name='job' id='job' style=absolute:fixed;left:200px;top:0px;visibility:hidden;z-index:0 src='' height="475" width="1150" scrolling='yes' frameBorder='0' ></iframe> <iframe name='jobed' id='jobed' style=position:fixed;left:460px;top:150px;visibility:hidden;z-index:10 src='' height="217" width="350" scrolling='no' frameBorder='0' ></iframe> <iframe name='newcat' id='newcat' style=position:fixed;left:460px;top:150px;visibility:hidden;z-index:10 src='' height="125" width="350" scrolling='no' frameBorder='0' ></iframe> </form> <?php error_reporting(E_ALL); include('dbcon/dbconnect.php'); $con=mysqli_connect($host,$user,$password,$db); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if (isset($_POST['id'])) { $insql= "INSERT INTO catagory (Catagory) VALUES ('$_REQUEST[newcat]')"; $con->query($insql); } $Catresult = mysqli_query($con, "SELECT CatID, Catagory FROM catagory"); echo "<div id='wb_CatSel' style='position:absolute;z-index:0;text-align:center;'bgcolor='#00b0e6';>"; echo "<span style='position:fixed;left:10px;top:82px;font-family:Arial;font-size:15px;background-color:#00b0e6;width:175px;'>Select Category</span></div>"; echo "<form action='jobs.php' target='joblist' style='position:fixed;left:10px;top:101px;'></td>"; echo "<select name='cat' size='11' style='width: 175px;' onchange='this.form.submit()'>"; while($row = mysqli_fetch_array($Catresult)) { echo "<option value=\"".$row['CatID']."\">".$row['Catagory']."</option>\n "; } echo "</select>"; echo "</form>"; echo "<form action='Jobnew.php' target='jobed' method='post'>"; echo "<div style='position:absolute;left:10px;top:285px;width:1345px;height:50px;z-index:0;'>"; echo "<Input type='hidden' id='id1'><Input type='submit' id='sbtn' style='width:175px;visibility:hidden;' Value='Add Job To Category'></div></form>"; echo "<form action='Jobtask.php' target='' method='post'>"; echo "<div style='position:absolute;left:10px;top:310px;width:1345px;height:50px;z-index:0;'>"; echo "<Input type='submit' style='width:175px;' action='Jobtask.php' Target='_top' method='post' Value='Reset Form'></div></form>"; echo "<form action='newcat.php' target='newcat' method='post'>"; echo "<div style='position:absolute;left:10px;top:335px;width:1345px;height:50px;z-index:0;''>"; echo "<Input type='submit' style='width:175px;'' action='newcat.php' Target='newcat' method='post' Value='Add New Category'></div></form>"; ?> </body> </html> I work with a large codebase and I have this situation come up fairly often:
the legacy class has large objects such as:
$design->motor->dimensions->a; $design->motor->dimensions->bd; $design->specifications->weight; $design->data->height; //etcWhen I create a new class, that class sometimes operates on the specific data, so say: class MyClass { public function compute($weight, $height, $a) { //stuff } } //and I call this for example as such: (new MyClass())->compute($design->specifications->weight, $design->data->height, $design->motor->dimensions->a);CONS: Potential issue: if design specs change and I need to change things parameters in "compute" function, I need to "re-key" the variables I pass to the function, and adjust things accordinly in MyClass as wel. PROS: only the exact data is being passed, and this data can come from anywhere it is viable, so in effect the function is fairly well separated, I think. Alternative option for me is to pass the entire design object, i.e class MyClass { public function compute(&$design) //can also be done via DI through a setter or constructor. { print $design->specifications->weight; print ($design->data->height + $design->motor->dimensions->a); //stuff } } (new MyClass())->compute($design);PROS: In this case when things change internally in which variables are needed and how things are computed, I only need to change the code in compute function of the class itself. CONS: MyClass now needs to be aware of how $design object is constructed. When it comes to this parameter passing, and Dependency Injection like this in general, does Computer Science advocate a preference on this issue? Do I pass the entire object, or do I pass only the bits and pieces I need? I'm trying to get a simple table to display and am not sure how to make it happen. Table will have just 2 columns. A TIME and an EVENT. What I want to have happen is for the first row to show the TIME and next to that, the EVENT. If a TIME has more than one event going I want the next row to show an empty first cell and then the second event below the first. If I set up the loop the only way I know how (so far) it would also output the TIME value again and again as many times as there were events for that same time. For example what I don't want is: 9:15 Sunday School 9:15 Nursery Available What I do want is: 9:15 Sunday School Nursery Available Thanks. I have a submit.php file that includes the following jQuery code: <script> [...] request.done(function( json ) { jQuery('input[name=thumbnail-url]').val(json.thumbnail_url); jQuery('input[name=job_title]').val(json.title); jQuery('textarea[name=htmlcode]').val(json.html); [...] </script> I need to remove jQuery('textarea[name=htmlcode]').val(json.html); and pass the "json.html" value into a PHP variable in another php file (functions.php). The code in the functions.php file is already there. The thing i need to do (and i am seriously struggling with it) is placing the value of json.html into a $phpvariable that i can then call from the function.php file. Do you need any more info for this issue? Thanks. |