PHP - Variable Not Being Passed To Include
Hi All, I've been trying to access xml data from an API feed, but it needs a Variable to be passed to an include file without a form. the include is at the header of the page. in the code below, i'm trying to retrieve the variable $project_add from the $xml_2 feed. $proj_add_ID comes from another feed and is being passed ok. foreach ($projects_1 as $proj_1) { $job_no =$proj_1['job_no']; $job_name =$proj_1['job_name']; $job_status =$proj_1['job_status']; $job_staff =$proj_1['job_staff']; $job_due =$proj_1['job_due']; $clr_1 = $colors_1[$job_status]; if ($err_2) { echo "cURL Error #:" . $err_2; } else { $_POST['job_id']= $job_no; $xml_2=simplexml_load_string($response_2) or die("Error: Cannot create object"); $proj_add = $xml_2->xpath("CustomFields/CustomField[ID ='$proj_add_ID']"); foreach($proj_add as $item_2) { $project_add = "$item_2->Text"; } echo "<div class='row no-gutters'>"; echo "<div class='col-sm bg-white border rounded'><a href='managejob.php?job_id=$job_no' class='text-left ml-1'><strong>$job_no</strong></a></div>"; echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_name</p></div>"; echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'></p>$project_add</div>"; echo "<div class='col-sm-2 bg-white border rounded'>" . state_dropdown_1($job_no, $job_status, $clr_1) . "</div>"; echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_staff</p></div>"; echo "<div class='col-sm-2 bg-white border rounded'><form action='CALL_API_PUTTaskDate.php' method='POST'>" . "<input type='hidden' name='task_ID' value ='". $jdata['task_ID'] ."'>" . "<input type='hidden' name='est_min' value ='". $jdata['est_min'] ."'>" . "<input class ='form-control bg-white text-dark font-weight-bold' type='date' name='start_date' value='" . date('Y-m-d', strtotime( $jdata['display_date_1'] )) . "' onchange='this.form.submit()'>" . "</form></div>"; echo "<div class='col-sm bg-white border rounded'><p class='text-left ml-1 mt-1'><strong>" . date('d/m/Y', strtotime( $jdata['display_date'])) . "</strong></p></div></div>"; } The include file is like this / collect Custom Felds if ($_SERVER["REQUEST_METHOD"] == "POST") { // collect value of status input field $job_no = $_REQUEST['job_id']; } if (isset($_POST['job_id'])) { $job_no = $_POST['job_id']; } if (isset($_GET['job_id'])) { $job_no = $_GET['job_id']; } $curl_2 = curl_init(); curl_setopt_array($curl_2, array( CURLOPT_URL => "https://api.xxx.com/job.api/get/$job_no/customfield?apiKey=xxx&accountKey=xxx", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Accept: */*", "Accept-Encoding: gzip, deflate", "Cache-Control: no-cache", "Connection: keep-alive", "Host: api.xxx.com", "Postman-Token: xxx", "User-Agent: PostmanRuntime/7.17.1", "cache-control: no-cache" ), )); $response_2 = curl_exec($curl_2); $err_2 = curl_error($curl_2); curl_close($curl_2); The problem is the variable $job_no isn't getting passed to CURLOPT_URL so it's not retrieving the data required. i need to be able to retrieve this data without submitting a form as it's in a foreach loop and the Id is specific to each iteration. is there a way to do this? I have been able to achieve this by putting the include within the div tags that it will be displayed but it just slows down everything too much and hangs. Any help will be much appreciated. Kind regards, Michael Bennington. Similar TutorialsI am trying to pass a php variable to javascript but it's not working. This is the "sketch" of my code. <?php $url="asset/go/"; ?>
<!DOCTYPE html> <html> <head> <title>Page Title</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> </head> <body> <script> var user="mayor"; var url ='<?php echo json_encode($url); ?>'; if(user=="mayor"){ alert(url); } </script> </body> </html> As it is now, it comes with the output below: <?php echo json_encode($url); ?> If I remove the quotation around the php in d jQuery, it throws an error in syntax. Also both codes are located in the same file named test.php Thanks in advance. Edited April 18 by Abel1416<?php global $places; $comm = $GLOBALS["location"]; $comm=trim($comm); while(list($one,$two) = each($places)) { echo "<option value=\"$two\""; if ( $two == $comm)echo "selected "; echo ">$two</option>\n" ; } ?> Variable passed to each() is not an array or objectHi, my code works fine but it is giving the error message Please can anyone help? Thanks i've been programming in PHP for years, and have done a substantial amount of work on applications of this nature. this problem has me stumped, not because i can't fix it (i did), but because i have no idea what the problem is. there are hundreds of lines of code involved here, so i'll break it down into a post-friendly format. take this for example, and forgive any typos. it's late, and i've been beating my head against this for over two hours... =\ this is from my form: Code: [Select] /* ... numerous form fields being passed as $_REQUEST arrays */ <input type="hidden" name="option_id[]" value="<?php print $query_result->option_id; ?>" /> /* a couple hundred more lines */ here's the DB update handler: Code: [Select] if (!empty($_REQUEST['option_name'])) { foreach ($_REQUEST['option_name'] as $k => $v) { if ($v != '') { $option_id = $_REQUEST['option_id'][$k]; $option_name = $_REQUEST['option_name'][$k]; $option_price = $_REQUEST['option_price'][$k]; $option_desc = htmlentities($_REQUEST['option_desc'][$k], ENT_QUOTES); if (!$option_id = '') { $sql_options = "UPDATE table SET" . " option_name = '" . $option_name . "', option_price = '" . $option_price . "', option_desc = '" . $option_desc . "' WHERE option_id = '" . $option_id "'"; if (!$query_function($sql_options)) { $error = true; } } else { $sql_options = "INSERT INTO table (option_name, option_price, option_desc)" . " VALUES ('" . $option_name . "', '" . $option_price . "', '" . $option_desc . "')"; if (!$query_function($sql_options)) { $error = true; } } } } } the above code doesn't post to the database because the $option_id variable returns a null value. however, if i replace the $option_id variable where i build the query string with $_REQUEST['option_id'], it works just fine. Code: [Select] /* in relevant part */ $sql_options = "UPDATE table SET" . " option_name = '" . $option_name . "', option_price = '" . $option_price . "', option_desc = '" . $option_desc . "' WHERE option_id = '" . $_REQUEST['option_id'] . "'"; needless to say i was infuriated by having spent a couple of hours to come to this conclusion. i only used the variables in the first place because i need to expand the function that this lives inside and i don't want to have to type $_REQUESTs over and over. the only thing i can think is that it might be a type issue. the data is coming out of the mysql table from an INT field and being placed into the value for the hidden field straight from the row collection. would forcing a variant data type by not strongly typing my variable have caused this problem? i haven't tested the theory because i'm still too ticked off to open my code editor. i'm bouncing this off the community and posting my experience in the hope that it might help someone who comes after. I actually asked a question here yesterday and decided to try a different route with it. What I am doing is passing an email variable entered from my home page on to www.dollapal.com/offerlist.php. I'm wanting this page to be a complete list of all of my entries in my surveys table. The email variable needs to be appended to the end of every link. I got that to work, but what I want to do now is display that information for every record in my 'surveys' table. Right now I am using the random function, which I'm sure is wrong, but I'm not sure what function to be looking for. Is it possible to use a foreach function here to echo each record? If so, I'm not sure how exactly to call the foreach() function in this case. I believe my two problems lie in the random and foreach functions, but I'm not sure how to correct them. I've attached a little chunk of code that I'm working with. I'm not sure if I'm completely off base here, or if I'm close to achieving my desired result. Please let me know if you require more information. This forum has been amazing to me so far. Thank you all for your help! hello, please i need ur help!!!! when i enter this url in my browser:http://localhost:10080/ntop/dumpData.html?language=php&proto i get: $ntopHash = array( 'ff02::1:2' => array( 'index' => '0', 'hostNumIpAddress' => 'ff02::1:2', 'hostResolvedName' => 'ff02::1:2', 'firstSeen' => '1337008485', 'lastSeen' => '1337009340', 'minTTL' => '0', 'maxTTL' => '0', 'pktSent' => '0', 'pktRcvd' => '15', 'ipBytesSent' => '0', 'ipBytesRcvd' => '0', etc...... so i'm writing a script to extract these informations and put them in a new web interface in a table. my script is: Code: [Select] <HTML> <HEAD> <LINK REL=stylesheet HREF=http://localhost:3000/style.css type="text/css"> </HEAD> <BODY BGCOLOR=red> <?php $host="localhost"; $port=10080; $url="http://localhost:10080/ntop/dumpData.html?language=php&proto"; $fp = fsockopen ($host, $port); if (!$fp) { echo "$errstr ($errno)<br>\n"; } else { $outStr = ""; fputs($fp, "GET $url HTTP/1.1\r\nHost: $host\r\n\r\n"); while (!feof($fp)) { $out = fgets($fp,128); if($out == "\n") $begin = 1; else if($begin = 1) $outStr = $out; } fclose ($fp); #echo "<pre>$outStr</pre>"; eval($outStr); } echo "<center>\n<table border>\n"; echo "<tr><th BGCOLOR=white>Sessions</th><th BGCOLOR=white>Values</th></tr>\n"; while (list ($key, $val) = each($ntopHash)) { echo "<tr><th align=center BGCOLOR=white>$key</th>\n"; echo "<td><table border>\n"; while (list ($key_1, $val_1) = each ($val)) if(gettype($val_1) == "array") { echo "<tr><th align=left>$key_1</th><td><table border>\n"; while (list ($key_2, $val_2) = each ($val_1)) { echo "<tr><th align=left>$key_2</th><td align=right> $val_2</td></tr>\n"; } echo "</table></td></tr>\n"; } else if($val_1 != "0") { if($key_1 == "sessionState") { if($val_1 == 0) $val2 = "SYN"; else if($val_1 == 1) $val2 = "SYN_ACK"; else if($val_1 == 2) $val2 = "ACTIVE"; else if($val_1 == 3) $val2 = "FIN1_ACK0"; else if($val_1 == 4) $val2 = "FIN1_ACK1"; else if($val_1 == 5) $val2 = "FIN2_ACK0"; else if($val_1 == 6) $val2 = "FIN2_ACK1"; else if($val_1 == 7) $val2 = "FIN2_ACK2"; else if($val_1 == $val2 = "TIMEOUT"; else if($val_1 == 9) $val2 = "END"; echo "<tr><th align=left>$key_1</th><td align=right> $val2</td></tr>\n"; } else echo "<tr><th align=left>$key_1</th><td align=right> $val_1</td></tr>\n"; } echo "</table></td></tr>\n"; } echo "</table>\n"; // echo "<pre>$outStr<pre>"; ?> </center> </body> </html> and i'm having this error: Sessions Values Warning: Variable passed to each() is not an array or object in C:\xampp\htdocs\projet\sessions.php on line 33 please i need your helpppp i get this error Warning: current() [function.current]: Passed variable is not an array or object.. for this Code: [Select] $lastblock = current( ${"s".$row} ); print_r($lastblock); when i change to this it works.. Code: [Select] $lastblock = current( $s0 ); print_r($lastblock); The problem is i won't know the $row seeing as it is in a while loop. Solution? Hi all I am writing a code to update customer details. It all works great until I get to the last line of code that directs the user to the next page: include ('profile-success.php'); I need it to be the following page: profile-success.php?username=$username Can anyone show me how to edit the line to include the url variable? I keep getting this error: Warning: include() [function.include]: Failed opening 'profile-success.php?username=$username' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/cndvizp/public_html/prints2impress.co.uk/upload/update-profile.php on line 96 Thanks Pete. SOLVED Hello I have a website that I am trying to upgrade so that all the data can be placed into a MySql database. I know what I need but I do not know if it is possible with PHP and MySql. I need to insert into a Href link a variable so that when the link is clicked it would initiate a MySql search for data. The variable would contain the Key id of the person whose name was clicked. This is very difficult to explain so a visit to the static website may help understand what I am trying to achieve, www.westonheritage.co.uk If you go to the second page by clicking the key then click on any of the surnames listed. I have the database setup on localhost and can get out the data I require. Kind regards I want to include a variable (for example an object $user) in each PHP page. Previously I declared $var in an include file then included that file in every page, however it generates an error in Zend Studio and it's also not very good programming style. I want to know how I can do it any other way. I was thinking of using constants but I don't know if constants should be used for this purpose and also I do not think constants can be defined as objects. Thank you very much for your help. My catalog uses a template file to list product details. One part of it is responsible for putting these details into nifty little tabs: <?php $template = '{magictabs style=black_rounded, tabwidth=110px}'; $template .= 'Technical Specifications'; $template .= '::'; $template .= '<table colspan="3"><tr><td>'; $template .= $details; $template .= '</td></tr></table>'; $template .= '||||'; ?> I don't want it to output $details, I want it to output a separate script instead. How do I include this separate file as a variable? I want this: <?php include('specs.php'); ?> to replace the $details variable. I am developing a page that displays info from a database based on a variable called "ID". On this page is a form that when submitted runs an external function file to first error check the form and then insert it into the database. The problem i'm having is if i include the page which had to form to display the error message on, it doesn't load the right info from the database because its not loading based on the ID. So is there an alternative way to write: include ("toDo.php?ID=".$ToDoID); Any help is much appreciated. Hello all, I am a beginner at PHP and trying to form an IF statement, I'm running into a problem which I cannot seem to solve. Due to my lack of syntax knowledge I would appreciate if someone could point me in the right direction. The combo variables are sent to this page through a form and defined as these variables at the top of the page. e.g $combo0 = $_POST["combo0"]; When the results are displayed any form field which was left blank or empty I want to show "N/A" if data is detected in being sent through the form I then want the "Else" statement to take affect. Currently when I run the below statement N/A is echoed despite if data was sent through the form fields or not.... if (empty($scrap)) { echo "<h3>Submitted Scrap Results</h3> No scrap parts were booked off"; } elseif (empty($combo0) || empty($combo1) || empty($combo2) || empty($combo3) || empty($combo4)) { echo "N/A"; } else { echo "<h3>Submitted Scrap Results</h3> <b>Reason:</b> " .$combo0." - ".$combo1." - ".$combo2." - ".$combo3." - ".$combo4." <br /> <b>on machine</b> " .$mid. " - <b>No. Scrap</b> " .$noscrap. ""; } Anyone any ideas? Ok it's abit complicated so i'll try to explain as best as I can. Upon registration a file is created with your username that was registered and also a file for your profile named after your username so it would be /profiles/usernamesprofilehere.php. I put a code into the profile that is: Code: [Select] include ("/home/giacjrdi/public_html/mysite/profiles/profileinfo/$username.info.php"); echo "<center><b><br /> $user's Profile <br /></b></center>"; $username is calling the cookie's username while $user is calling the account's username. eg if I try to view "bob"'s profile but logged in as "fred" since the include function is under $username which is cookies, it will retrieve "fred"'s information and post it on "bob"'s profile. The only sollution i can think of would be to store a username variable in the url and use it for the include function. Hello! I want to be able to make a code that tells me how many saturdays have passed till today. I've tried a couple of things but nothing. Any help?! Thanks! In my main script I set $errorArray with values when required survey questions go unanswered. The ne code that checks for missing required values is working perfectly, and right before I pass $errorArray to a function which rebuilds the survey questions - this time with the error messages - I do a var_dump($errorArray) and that array has errors in it as expected. But while my function that re-builds the survey questions gets called and does indeed re-build the survey questions, the $errorArray isn't getting pased for some strange reason? (I'm not sure if i have ever passed an array to a function before.) In my function I have... function generateSurveyItem($dbc, $questionNo, $questionID, $surveyResponseArray=NULL, $errorArray=NULL){ }
When I var_dump($errorArray) at the top of the function, and submit a blank form, I end up with a blank page and a file path to my PHP script ending with... /public_html/utilities/function.php:3010:null
Questions: 1.) What is happening?
2.) I thought =NULL in the parameter list just meant that the argument is "optional"?
3.) I am able to successfully pass $surveyResponseArray to this function, so what is up with $errorArray?
P.S. When I run things, and submit an empty form - thus no "required" fields get completed - the survey gets re-displays, and any values chosen are "sticky", but my error messages should also be displayed to let the user know what is mising, and that is the part that isn't working? This has to be something silly stupid to fix... Edited February 16 by SaranacLake Hi I am trying to get all variables passed in a url string and wondered how would i do it? I dont know what they are yet and i need to set key as value. Any help? Hi I use Dreamweaver to do a lot of my work on a php/mysql database. I have two tables - applications and contacts and they are linked by the id appid. When I view a page with all of the application and contact details the url contains the appid as a number at the end (eg .../Summary.php?appid=639) When I want to edit one of the contacts I go to the edit page but this is identified by a contact id so that url looks like: ../EditContact.php?contactid=32 When I update the contact's details I would like to return to the summary page but can't as the url gets muddled between the two different ids so I end up at a page with no info on it. How can I get round this? I'm sorry my explanation isn't very clear but any help is much appreciated. Good day freaks. I am pulling my hair out trying to figure this out. I am trying to pass patterns for preg_replace through $_GET and then apply them. The object is to highlight certain words in a chunk of text. Say I want to highlight bounded cases of 'foo' in the text. So here's what I'm doing: Code: [Select] $string = urldecode($_GET['highlight1']); // $string contains \\bfoo\\b $search = preg_replace('/\\\\b/','b',$string); // $search contains \bfoo\b as I want it to $replace = preg_replace('/\\\\b/','',$string); // $replace contains \foo\ >:-( $pattern = '/'.$search.'/i'; $text = preg_replace($pattern,'<span style="background-color:yellow;">'.$replace.'</span>',$text); The problem is that no matter what I try for the pattern for $replace, it winds up with backslashes around foo. How do I get rid of these? (I should say that I discovered the pattern/replace for the $search string by experimentation and I don't understand why the pattern doesn't pick off all the backslashes in that case--but at least it does what I want). TIA Steve Hi all, How can I check in PHP if this date has already passed in the following format: 2012-12-12 |