Jump to content

ploppy

Members
  • Posts

    59
  • Joined

  • Last visited

Everything posted by ploppy

  1. I have some code where a user can enter a file name/number separated by a comma. At the moment, I am checking for duplicate entries against a MySql database, which works well. However, I also need a way to check if a user has input the same entry so upon submission, example, a1b2c3,a1b2c3. I can catch this and trigger or echo an alert and halt the execution of the script. If there are no dupe entries, proceed to check db. I have been struggling with this and need to find a way to check before the check db code.I turn to the exerts for some guidance/assistance with this. I would be grateful for any help with this so I can move forward and learn something new in the process. Many thanks. <?php if (isset($_POST['file_add'])) { if (!empty($_POST['file_add'])) { $filedata = split(',', $_POST['file_add'][0]); $fileadd = array(); $fileduplicates = array(); foreach ($filedata as $val3) { if ($val3 != "") { $sql = "SELECT custref FROM files WHERE custref='$val3' AND customer = '" . $_SESSION['kt_idcode_usr'] . "'"; $qry = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($qry)) { $fileduplicates[] = $val3; } else { $fileadd[] = $val3; $flag = 1; } } else { $fileerror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'FILE ERROR! ' . '</span>' . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . 'You must enter a file for intake' . '</span>'; echo "<script language=\"JavaScript\">\n"; echo 'alert("FILE ERROR:\nYou must enter a file for intake.");'; echo " </script>"; echo $fileerror; } } if (count($fileadd)) { echo 'You wish to add file(s): ' . ' <div style="word-wrap:break-word;white-space: pre-wrap;overflow:auto !important;height: 100px; width: 250px; border: 1px solid #666; background-color: #fff; padding: 4px;">' . '<span style="font-weight:bold;color: #000;">' . join(', ', $fileadd) . '</span>' . '</div> ' . ' <p /> '; //if(count($duplicates) == 0) // add to session only if we have no duplicates $_SESSION['file_add'] = $fileadd; /// change here, do not store $_POST since it can contain duplicates } if (count($fileduplicates)) { $filedupeerror = "ERROR:" . " " . "File" . " " . '(' . join(', ', $fileduplicates) . ')' . " " . "This file is already in the database"; echo "<script language=\"JavaScript\">\n"; echo 'alert("FILE ERROR:\nThis file is already in the database");'; echo " </script>"; echo $filedupeerror; } } } ?>
  2. I am at a loss why my query is not inserting values into db. Even if I do echo $query or var_dump($query) there is nothing printed at all. All values are being passed successfully just not being inserted. I am getting 'Could not connect' but I do not know why. All connections are established and as a test I took this code and ran it on it's own with dummy data and it inserted the data fine. I can only think it has something to do with the $response_array. Where am I going wrong. and would appreciate any help. Thanks <?php require_once('Connections/sample.php'); ?> <?php session_start(); $new = 1; $activity = 'General Contact Enquiry'; $mobile = 'Submitted from mobile'; $name = mysql_real_escape_string($_POST['GC_name']); $department = mysql_real_escape_string($_POST['GC_department']); $message = mysql_real_escape_string($_POST['GC_message']); $email = mysql_real_escape_string($_POST['GC_email']); $company = mysql_real_escape_string($_POST['GC_company']); $position = mysql_real_escape_string($_POST['GC_position']); //response array with status code and message $response_array = array(); //validate the post form //check the name field if(empty($name)){ //set the response $response_array['status'] = 'error'; $response_array['message'] = 'Name cannot be blank'; //check the name field } elseif(empty($company)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a company name'; //check the position field }elseif(empty($position)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a position'; //check the email field } elseif(empty($email)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a valid email address'; //check the dept field }elseif($department=="Choose Department") { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must select a department'; //check the message field }elseif(empty($message)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a message'; //check the dept field } else { //set the response $response_array['status'] = 'success'; $response_array['message'] = 'Your enquiry has been sent succesfully'; $flag=1; } //send the response back echo json_encode($response_array); if($flag == 1) { mysql_select_db($database_sample, $sample); $query = 'INSERT INTO feedback (company, department, name, email, position, feedback, date, new) VALUES (\''.$company.'\', \''.$department.'\', \''.$name.'\', \''.$email.'\', \''.$position.'\', \''.$message.'\', NOW() , \''.$new.'\')'; mysql_query($query) or die("Could not connect"); } ?>
  3. hello. I seemed to have got lost with my if statements here and cannot see where to use elseif . I am getting the duplicate response message ok, but if I submit with blank input, it produces NaN error, which I know is JavaScript but I think it is because of the order of the if statements. If someone could help me with this I would be very grateful. Thanks. if (!empty ($_POST['item'])) { $data = split(',',$_POST['item'][0]); $duplicates = array(); foreach ($data as $val) { if ( $val != "" ) { $sql = "SELECT custref FROM boxes WHERE custref='$val' Union SELECT item FROM act WHERE item='$val'"; $qry = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($qry)) { $duplicates[] = $val; } } } if(count($duplicates)) { $response_array['status'] = 'error'; $response_array['message'] = 'No duplicate files'; } } //check the name field elseif(empty($authorised)){ //set the response $response_array['status'] = 'error'; $response_array['message'] = 'Name cannot be blank'; //check the service field }
  4. With no value entered into the form, this is the result of the test you asked me to perofrm: Also, I am using jquery serialize() to collate form data. Thanks [carton] => Array ( [0] => [1] => )
  5. @Pandemikk very simply, it is a empty array. if I test for empty $_POST using the following (The elseif is there because it is in the middle of other elseif statements), then no error is triggered, but it should be because the input on the form is empty. But if I use !empty, then the error is triggered. Why is that happening. Thanks elseif(empty($carton)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a carton number'; }
  6. I get this carton[0]:carton[1]. From what I can see, it is just an array that can be processed using foreach. But there is no values to extract. So I guess my question would be: What do I test for as a condition for the error checking. Thanks
  7. I am trying to code a error callback using php and ajax. What I cannot get past is, I am assinging a variable to a $_POST in PHP to check for an empty value. If empty, then trigger error message. However, jquery is passing the post to php as carton%5B0%5D:carton%5B1%5D:. Would php see this as an empty post? What are these numbers etc after carton? carton is actually an array. If I do elseif(!empty($carton)) then the message is triggered. This is strange bearing in mind that the input values are empty at submit stage. thanks This is the code that jquery uses to create an input from a slider change event. for(var i = 0;i < $(this).val();i++) { $("#carton").append('<div data-role="fieldcontain"><label for="carton" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="carton['+i+']" id="carton['+i+']" class="carton ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" /></div>') } sample php code $carton = $_POST['carton']; elseif(empty($carton)) { //set the response $response_array['status'] = 'error'; $response_array['message'] = 'You must enter a carton for retrieveal'; //if no errors }
  8. You would need to use a library like jquery. You could also use there serialize function which would process all form elements then submit to php using ajax.
  9. @pikachu That is because I am only using json to return the data to ajax. I have $_POST variables in the code that I use to insert into database. $box is an array when it is passed from ajax using jquery serialize function. Thanks
  10. here you go. sorry for the delay. Thanks $query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`) VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($box).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');'; mysql_query($query) or die('Error, query failed');
  11. do I do that on the $_POST before the json array or after? thanks
  12. I am using jquery to send the data through ajax. for(var i = 0;i < $(this).val();i++) { $("#BRVbrtrv_boxnumber").append('<div data-role="fieldcontain"><label for="BRVbrtrv_boxnumber" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="BRVbrtrv_boxnumber['+i+']" id="BRVbrtrv_boxnumber['+i+']" class="BRV_boxnumber ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" /></div>') }
  13. also, I get this error when applying escape string to post $boxnumber = mysql_real_escape_string($_POST['BRVbrtrv_boxnumber']); <b>Warning</b>: mysql_real_escape_string() expects parameter 1 to be string, array given in <b>
  14. It is still inserting as 'ARRAY'. I did not understand your question. are you referring to the ajax code and the db query?
  15. Is this what you need? Thanks <pre>Array ( [activity] => Box Retrieval [mobile] => Submitted from mobile [company] => DEMO [authorised] => DEMO [service] => standard [department] => DEMO [address] => 28 mile end road london e1 7yu [boxcount] => 2 [boxnumber] => Array ( [0] => ttt [1] => fff ) ) string(323) "a:9:{s:8:"activity";s:13:"Box Retrieval";s:6:"mobile";s:21:"Submitted from mobile";s:7:"company";s:4:"DEMO";s:10:"authorised";s:4:"DEMO";s:7:"service";s:8:"standard";s:10:"department";s:4:"DEMO";s:7:"address";s:32:" mile end road london e1 7yu";s:8:"boxcount";s:1:"2";s:9:"boxnumber";a:2:{i:0;s:3:"ttt";i:1;s:3:"fff";}}" </pre>
  16. this then displays a:9:{s:8:"activity";s:13:"Box Retrieval";s:6:"mobile";s:21:"Submitted from mobile";s:7:"company";s:4:"DEMO";s:10:"authorised";s:4:"DEMO";s:7:"service";s:8:"standard";s:10:"department";s:4:"DEMO";s:7:"address";s:32:"28 mile end road london e1 7yu";s:8:"boxcount";s:1:"2";s:9:"boxnumber";a:2:{i:0;s:3:"rrr";i:1;s:3:"eee";}}. how do i take that data and use it to insert into db? Thanks
  17. @AyKay47 I am a noob so could you perhaps get me started with how serialize() would apply to my code? many thanks
  18. 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 $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;
  19. I am really stuck with this guys. Any further help please? Thanks
  20. is there an alternative with my version? thanks
  21. hi. I am getting error using that code: <b>Fatal error</b>: Call to undefined function json_encode() in line 66 which is: $json_string = json_encode($json);. I am using php 5.1.6. Thanks
  22. I did a var_dump($_POST['BRVbrtrv_boxnumber']); and this is the result. array(2) { [0]=> string(3) "eee" [1]=> string(3) "ddd" } { "boxnumber": "eee", "boxcount": "2" } { "boxnumber": "ddd", "boxcount": "2" }
  23. FYI. If i take $json = ""; out of the loop in the, content tab after the alert error, the correct values are present. { "boxnumber": "rrr", "boxcount": "2" } { "boxnumber": "ttt", "boxcount": "2" }
  24. basically, the same tabs as firebug. This is the result in the json tab if i put it back: boxcount: "2" boxnumber: "iii" and i am inputting 2 values, should be iii, eee. I am developing using jquery mobile and this doesn't want to play ball in firefox. Dosen't show the layout so cannot enter any values.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.