PHP - Funny: $_get[] Passed Only When Refreshed.... :-(
Folks,
I am trying to grab the value selected in a Dropdown, with $_GET['q']. Then i am echoing the grabbed value. Funny thing is, when i select value from HTML dropdown (onchange="location=this.value"), and when a new value is selcted, it appaears in url like this: Quote http://www.abc.com/?search=selected-value.html But when i try to grab and echo the value of 'q' it gets passed only when i refresh the page and not when i change my selecting in dropdown. Am not sure whats happening, can someone help me on this? Cheers NT Similar TutorialsGood 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 A few months ago, and a good amount of time before that, I had people telling me to use isset() instead of performing to see if the variable is empty, such as: !$_GET[''] I know the differences in the function and what they do, but when could isset() be used in a situation where it's better/more efficient then: !$_GET[''] I do use isset(), though. Wonder if any of you had this before. I'm pulling data from db for given day, and display them in a table. After, as extra feature, I'm running a quick query checking how many record are in the db for given day and dispaying result. The count works correctly - it shows the actual number of records in db, but the first part of the code - the listing of record - always skips the first record. So the count returns i.e. 7 record, but on the listing shows only 6. Here's the listing part of the code. Also you might notice a bit "messy" use of odbc_fetch_row and odbc_fetch_array, but that had to do with controlling situation when there was no records in db - I'll clean it up later Any ideas or solutions welcomed echo "<table border=0 class=\"report-font-table\"><tr bgcolor=#CCCCCC><td><b>MODEL</b></td><td><b>SERIAL NUMBER</b></td><td><b>INSPECTOR</b></td><td><b>COMMENTS/FAILS</b></td></tr>"; $MySQL1 = 'select Model, Serial_no, Inspector, Comment from CM_Audit where Date=#'.$new_date.'#'; $MyCon=odbc_connect('SQA_Typewriter','','') ; // use the SQA_Typewriter ODBC $result=odbc_exec($MyCon,$MySQL1); $check1=odbc_fetch_array($result); if (!empty($check1)) { while (odbc_fetch_row($result)) { echo "<tr> <td>".odbc_result($result,"Model")."</td> <td >".odbc_result($result,"Serial_no")."</td> <td >".odbc_result($result,"Inspector")."</td> <td >".odbc_result($result,"Comment")."</td> </tr>"; } echo "</table>"; odbc_close($MyCon); } else { echo "</table><p style=font-weight:bold;color:006699>No audits have been carried out on this day.</p>"; } How can I detect if the page was refreshed? I thought the following worked, however, after much head scratching, discovered it does not work for a POST request using the Chrome browser. Thanks
<?php $refresh=isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0'; echo(($refresh?'refresh':'not refresh').'<br>'); echo('$_GET<pre>'.print_r($_GET,1).'</pre>'); echo('$_POST<pre>'.print_r($_POST,1).'</pre>'); ?> <ul> <li><a href="refresh.php?id=1">one</a></li> <li><a href="refresh.php?id=2">two</a></li> <li><a href="refresh.php?id=3">three</a></li> </ul> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="text" value="hello"> <input type="submit" name="submit" value="submit"> </form> Here is the code in question: <form method="post" action="../php/sendmail.php"> <?php function RandomLine() { $textfile = "../messages/compliments.txt"; if(file_exists($textfile)) { $compliments = file($textfile); $string = $compliments[array_rand($compliments)]; } else { $string = "Error"; } return $string; } $line = RandomLine(); echo "<p style='color: red;'>"."$line"."</p>"; ?> <br> <input type="hidden" name="email" value="email@email.com"> <input type="hidden" name="subject" value="random compliment generator"> <input type="hidden" name="message" value="$line"> <input type="hidden" name="to" value="5555555555@mms.uscc.net"> <input type="submit" name="sendtext" value="Send to phone!"> </form> And here is the php from sendmail.php: <?php $email = $_REQUEST['email']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; $to = $_REQUEST['to']; $sent=mail($to, $subject, $message, "From: $email"); if($sent)echo "<br><p>The message was sent!</p>"; else echo "<br><p>There was an error while sending the message.</p>"; ?> When the form from the first bit of code is opened in an iframe, the echo displays the correct code but if the button is sent, the sms/email received simply says "$line". Any ideas? I am trying to run the following code -------------------------- $result = mysql_query("SELECT * FROM indiatutors_profiles WHERE id='$id' ") or die(mysql_error()); while($row=mysql_fetch_array($result)){ $email =$row[email]; } echo $email; ---------------------------- It gives me the following error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in website_path/profiles.php on line 7 The funny thing about the code is that inspite of giving the error, it outputs the email correctly based upon the id which it shouldn't have done if their was some error Hi guys, On my website i have a message inbox, and the code below is used to display and delete messages. When i tick the checkbox and click the delete button, the message gets deleted. The page appears to refresh after the submit button is clicked, however the message remains in the inbox, i have to refresh the page a second time for the message to disappear. Im trying to get it that when i press the submit button and the page reloads, the message disappears. Does anybody know of a technique that im not using or does anybody know if the code im using is wrong? <?php $messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'"); if (mysql_num_rows($messages_query) > 0) { echo "<form method='POST' action='inbox.php'>"; while ($messages_row = mysql_fetch_array($messages_query)) { if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; } $message_id = $messages_row['id']; echo "<a href='message.php?id=$message_id'>"; echo "From: " . $messages_row['sender']; echo "Subject: " . $messages_row['subject'] . "<br />"; echo "</a>"; echo "<input type='checkbox' name='message[]' value='$message_id' />"; if ($messages_row['message_read'] == 0) { echo "</div>"; } } } else { echo "No messages"; } if ($submit) { foreach ($_POST['message'] as $msgID) { mysql_query("DELETE FROM messages WHERE id='$msgID'"); } } ?> <html> <input type="submit" name="submit" value="Delete" /> </form> </html> Many thanks i have a problem i just noticed....i've built a shopping cart that totals quantities of items and shipping etc then a submit button to a payment page... but if a user refreshes the same page after first coming to the cart page it messes up all the totals and data..... i know this has to be a common problem with webpages... does anyone have any advice on combating this issue? any advice is much appreciated..thanks! Hello all, As the title says: For sake of example, let's say I have a form with a couple text boxes and a submit button. When you hit submit, the data from the text boxes is translated into a database record. Howver, refershing the page will this record again and again -- which I do not want. How can I prevent this? Keeping in mind the business logic of my applicatoin allows the same record to be entered twice -- however, it should only happen if the user intentionally visits the form agian, and enters the same data. It should not happen on a page refresh. I assume this is a common problem...? Any thoughts? Thanks! Hi Guys, Im trying to create a registration form so users can sign up to my website, however i have noticed that when the page is refreshed the form automatically submits. Please could somebody help me stop this? Thanks <body> <form name="registration_form" method="post" onsubmit="return Validate();"> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name"><br /> Email: <input type="text" name="email"><br /> Confirm Email: <input type="text" name="confirm_email"><br /> Username: <input type="text" name="username"><br /> Password: <input type="password" name="password"><br /> Confirm Password: <input type="password" name="confirm_password"><br /> <input type="submit" value="Register"> </form> <?php $connect=mysql_connect("localhost","leemp5_admin","p7031521"); mysql_select_db("leemp5_database",$connect) or die (mysql_errno().":<b> ".mysql_error()."</b>"); $insert_query = 'insert into users (username,first_name,last_name,email,password) values ( "' . $_POST['username'] . '", "' . $_POST['first_name'] . '", "' . $_POST['last_name'] . '", "' . $_POST['email'] . '", "' . $_POST['password'] . '" )'; mysql_query($insert_query); ?> </body> when the user clicks the submit button from a html form, it needs to go back two pages with that loaded page refreshed. for the javascript i only have history.go(-2); with php, I have tried cookies and sessions which i discovered that neither will work because the loaded page is not refreshed. I could do a html body onload but that would break the w3c validator. what are my options?
When I use the following code, it doesn't print out "test" until after I refresh the page. unset($_SESSION["product"]);
if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { When I use: var_dump($_SESSION["product"]); It shows: array(0) { } I'm wondering it that's the problem. That it's array(0) when it should be array(). 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! 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. Hi all, How can I check in PHP if this date has already passed in the following format: 2012-12-12
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. 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 I 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 Abel1416Hi, I want to update a row when 30sec are passed on a page. If not nothing happens(not updating row). Example: You go to example.php>> 30sec pass, update row>> 30sec not over, no update. it does not necessarily have to be second it can be minutes or hours. Who can help me? Hope its clear enough, Thanks. 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? |