Jump to content

Adamhumbug

Members
  • Posts

    597
  • Joined

  • Last visited

Everything posted by Adamhumbug

  1. Hi All, Sorry but i am really struggling with this...damn arrays. I dont think this option is going to work for me as the user has the ability to add as many fields as they want and move them around in order - the order is important so i would have to be constantly updating field names based on the order. The method which i am using now posts them in order negating this step. What is being posted is: array(9) { ["formName"]=> string(1) "q" ["formId"]=> string(0) "" ["formDescription"]=> string(1) "q" ["field"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } ["fieldName"]=> array(2) { [0]=> string(11) "Custom 1111" [1]=> string(8) "Custom 2" } ["fieldLabel"]=> array(2) { [0]=> string(8) "Custom 1" [1]=> string(8) "Custom 2" } ["fieldType"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } ["fieldWidth"]=> array(2) { [0]=> string(1) "6" [1]=> string(1) "6" } ["ajax"]=> string(23) "saveFormAndCustomFields" } I am trying to get the ids of the fields into the json column in my database ( to be clear i am able to do this but the keys are the issue). The data that is being posted - for example the below: ["field"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } using the following code if (!$_POST['formId']) { $sql2 = "INSERT INTO form (name, description, event_site_attachment_id, field_selection) VALUES (:name, :description, :esId, :fieldSelection)"; $stmt2 = $pdo->prepare($sql2); foreach ($_POST['field'] as $k => $v) { $fieldSelection[$k] = $v; } $stmt2->execute([ ':name' => $_POST['formName'], ':description' => $_POST['formDescription'], ':esId' => $_GET['esId'], ':fieldSelection' => json_encode($fieldSelection) ]); $out = $pdo->LastInsertId(); return $out; } is putting the following into the database ["1","2"] This is not unexpected based on the keys and values. What i would like to be in the database is something like the following { "formFields": [ { "id": 1, }, { "id": 2, }, { "id": 3, } ] } I am also posting other values that i will want to be in here at some point such as fieldWidth I think i am clearly misunderstanding the help that i have been given here so i apologise for that.
  2. This is what is posted. array(9) { ["formName"] => string(3) "asa" ["formId"] => string(0) "" ["formDescription"] => string(5) "asasa" ["field"] => array(2) { [0] => string(1) "1" [1] => string(1) "2" } ["fieldName"] => array(2) { [0] => string(11) "Custom 1111" [1] => string(8) "Custom 2" } ["fieldLabel"] => array(2) { [0] => string(8) "Custom 1" [1] => string(8) "Custom 2" } ["fieldType"] => array(2) { [0] => string(1) "1" [1] => string(1) "2" } ["fieldWidth"] => array(2) { [0] => string(1) "6" [1] => string(1) "6" } ["ajax"] => string(23) "saveFormAndCustomFields" }
  3. OK, so i have got data going in which is great. I am trying to get the key in the array as well as the value (it should be fieldId) - currently when submitting 2 objects with id 1 and 2 i am getting ["1","2"] The function is below if (!$_POST['formId']) { $sql2 = "INSERT INTO form (name, description, event_site_attachment_id, field_selection) VALUES (:name, :description, :esId, :fieldSelection)"; $stmt2 = $pdo->prepare($sql2); foreach ($_POST['field'] as $k => $v) { $fieldSelection[$k] = $v; } $stmt2->execute([ ':name' => $_POST['formName'], ':description' => $_POST['formDescription'], ':esId' => $_GET['esId'], ':fieldSelection' => json_encode($fieldSelection) ]); $out = $pdo->LastInsertId(); return $out; }
  4. turn out the show modal needed to be in the success of the ajax - makes perfect sense.. function generateFormModal($formId) { console.log($formId) $.ajax({ type: 'post', data: { 'ajax': 'generateFormModal', 'formId': $formId }, success: function(resp) { $('#createNewFormModal').html(resp) $('#createNewFormModal').modal('show') } }) }
  5. HI All, I have a modal and i want the content to be populated dynamically with info from the database. If the Blank Form button is clicked the same modal is generated but with no content. I though ajax would be the way to do this. $('#openNewFormModalButton, .editFormModalShow').on('click', function() { if (!$(this).data('form-id')) { $formId = null; } else { $formId = $(this).data('form-id'); } generateFormModal($formId); }) function generateFormModal($formId) { console.log($formId) $.ajax({ type: 'post', data: { 'ajax': 'generateFormModal', 'formId': $formId }, success: function(resp) { $('#createNewFormModal').html(resp) } }) $('#createNewFormModal').modal('show') } I have checked the console logs and the correct $formId is being passed, currently either null or 1. I have the container for the content of the modal. <div class='modal modal-xl fade' id='createNewFormModal' tabindex='-1' aria-hidden='true'> </div> I have the switch to call the php function if (isset($_POST['ajax'])) { switch ($_POST['ajax']) { case 'generateFormModal': exit(generateFormModal($pdo, $_POST['formId'])); break; } } and i have the function in php function generateFormModal($pdo, $formId) { if ($formId != null) { $sql = "SELECT name, description from form where id = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':id' => $formId ]); $data = $stmt->fetch(); $formName = $data['name']; } $out = " <div class='modal-dialog'> <div class='modal-content'> <form class='needs-validation' novalidate> <div class='modal-header'> <h5 class='modal-title' id=''>Form Management</h5> <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button> </div> <div class='modal-body'> <form id='form-management'> <div class='row mb-3'> <div class='col-12'> <label for='formName'>Form Name</label> <input id='formName' type='text' class='form-control' name='formName' value='$formName'> </div> </div> <div class='row mb-3'> <div class='col-12'> <label for='formDescription'>Form Description</label> <textarea id='formDescription' class='form-control' name='formDescription'></textarea> </div> </div> <div class='row mb-3'> <div class='col'> <div class='btn btn-primary' id='addNewFieldRow'>Add New Row</div> </div> </div> <div class='row'> <div class='row mb-1'> <div class='col'> <label for='field'>Field</label> </div> <div class='col'> <label for='newName'>Field Name</label> </div> <div class='col'> <label for='fieldLabel'>Field Label</label> </div> <div class='col'> <label for='fieldType'>Field Type</label> </div> <div class='col-2'> <label for='fieldWidth'>Width</label> </div> <div class='col-1 text-center'> <label for='fieldOrder'>Order</label> </div> </div> </div> <div class='row mb-3' id='sortable'> <div class='row mb-1 field-row'> <div class='col'> <select id='field' class='form-select field' Name='field[]'> <option value='0' selected disabled>Please Select</option> <?= getCustomFieldOptions(\$pdo) ?> </select> </div> <div class='col'> <input id='fieldName' type='text' class='form-control' name='fieldName[]'> </div> <div class='col'> <input id='fieldLabel' type='text' class='form-control' name='fieldLabel[]'> </div> <div class='col'> <select id='fieldType' type='text' class='form-select' name='fieldType[]'> <?= getFieldTypes(\$pdo') ?> </select> </div> <div class='col-2'> <input id='fieldWidth' type='number' class='form-control' min='1' max='12' name='fieldWidth[]'> </div> <div class='col-1 text-center handle'> <svg class='icon icon-xl' style='width:2rem;height:2.5rem;'> <use xlink:href='img/svg/free.svg#cil-move'></use> </svg> </div> </div> </div> <div class='row'> <div class='col-10'> <div class='alert alert-success alert-container'></div> </div> <div class='col-2'> <button type='submit' class='btn btn-primary w-100' id='saveForm'>Button</button> </div> </div> </form> </div> <div class='modal-footer'> <button type='button' class='btn btn-secondary' data-bs-dismiss='modal'>Close</button> <div class='btn btn-primary' name='addNewPassType' id='addNewPassType'>Save changes</div> </div> </form> </div> </div> "; return $out; } My issue is that when i run this all together, whether the form id is set to null or 1 the screen greys, showing that the modal is trying to load but there is no other content or actual modal shown. I am also getting a not that helpful JS error of: Uncaught TypeError: 'querySelector' called on an object that does not implement interface Element. When i inspect all of the modal content is there but nothing shown on the screen other than a darkened background. Any pointers for this error?
  6. If i go this way - do i not need a function for each value that just pulls that specific value that is being called. Will that not become a bit overkill if i have a massive form that ends up needing many individual queries to pull each value one by one?
  7. htmlentities() - should i have done this everytime i set a value with php? I have never done this - lots to change if this is the case.
  8. I always thought you were generating these from somewhere else!
  9. Thanks so much for this - i think it answers a lot of questions that i would have no doubt have been asking later. I feel like it will be a good use for JSON as i dont know how many entries there will be or what they will be. I will give it a go!
  10. I have a form that allows a user to populate the fields and add the data to the database. I am wanting to use the same code to edit the same information later. Imagine this code: ... $formName = $data['name']; $resp=" <div class='col-12'> <label for='formName'>Form Name</label> <input id='formName' type='text' class='form-control' name='formName' value='$formName'> </div> "; ... if i remove the $formName from the value the form is created perfectly but in order to populate it when there is already data and i am looking to edit, i need to have the variable in there. I understand that the variable is not set, as in this case there is no data for it to be set to but i have tried things like: $formName = $data['formName'] ?? ""; but i am still getting the undefined message. Is there a proper way to deal with this?
  11. A simple example of this could be: FieldName Field Label Width Order First Name Given Name 6 1 Last Name Surname 6 2 Should i have a link table that has a new row per field added to the form - something like this: form_id field_id 1 1 1 2 1 3
  12. This Will Be Really Handy THANKS!!!!!
  13. Hi, Would it be possible to have the ability (or instructions if it exists) to make a table in the editor. Often when trying to explain hypotetical table layouts it would be handy to be able to lay this out in table format. I love this forum and the people on it but this has troubled me for some time. Kind Regards
  14. I am making an application where the user can make a form. The first pick a form name and a description (to be saved to the forms table) Then they select the fields that they want to use from various drop downs, set the label and some bootstrap size properties with the aim being that once all of the fields have been chosen i will be able to create a form to show to end users. A simple example of this could be: Field Name ------ Field Label ------ Width ------ Order First Name ----- Given Name ----- 6 ----- 1 Last Name ----- Surname ----- 6 ------ 2 (i wish i could make tables on here) When these fields have been selected and related data amended, i am wanting to store them in the database so that i can query the data and build the form. I have been thinking about the best way to do this as each form with have a different and unknown number of fields. Should i have a link table that has a new row per field added to the form - something like this: form_id ----- field_id 1 ----- 1 1 ----- 2 1 ----- 3 or should i look at storing the fields as JSON in the form table itself. I have never gone down the JSON route and dont know how to interact with it or if it is a good route. Advice here would be appreciated.
  15. Thanks for this - i will look into what you have suggested when i get further through the project.
  16. In my application, i want to be able to create web portals that can be accessed from the internet - the user will need permission to get to the portal and will need to log in. In order to achieve this i think i need to create a folder in my file structure dynamically. So for example, you name the website "portal1" click "make new website" and a folder called "portal1" is ceated in the file structure and several files dropped into it. Is this possible and if so how would i go about it - or as always, is there another way i should be dealing with this?
  17. You are a wonderful human.
  18. $("#newZones").submit(function(ev) { ev.preventDefault() let fdata = $(this).serialize() + "&ajax=addNewZones"; $.ajax({ type: 'post', data: fdata, success: function(resp) { console.log(resp); $('.alert-container').html(resp).show() // if (resp == "User Created") { // $(':input', '#createNewUser') // .not(':button, :submit, :reset, :hidden') // .val('') // .prop('checked', false) // .prop('selected', false); // } } }) }) function addNewZones($pdo) { $sql = "INSERT INTO zones (name, print_value, event_site_attachment_id, zone_type_attachment_id) values (:name, :printVal, :siteId, :zTypeId)"; $stmt = $pdo->prepare($sql); echo "<pre>"; var_dump($_POST); echo "</pre>"; } This is now showing what looks like the right output array(4) { ["zoneType"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["zoneName"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "3" } ["printValue"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } ["ajax"]=> string(11) "addNewZones" } How does one go about looping through this array to insert it into the DB but only when a zone name is present.
  19. is that becuase my code is "wrong" - should i be switching to your way that works or is there another way around.
  20. in the function to insert the data function addNewZones($pdo, $zoneArr){ foreach ($zoneArr as $k => $v) { } } is throwing : foreach() argument must be of type array|object, string given
  21. This seems to suggest that i have got it how i need it - i think function addNewZones($pdo, $zoneArr) { $params = array(); parse_str($zoneArr, $params); echo ("<pre>"); var_dump($params); echo "</pre>"; } array(3) { ["zoneType"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" } ["zoneName"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "3" [2]=> string(1) "5" } ["printValue"]=> array(3) { [0]=> string(1) "2" [1]=> string(1) "4" [2]=> string(1) "6" } }
  22. Thanks for this. I will certainly have a look at <template> and great tip on the lables.
  23. Amazing, i have modified your code a little so that it fits with how i have done everything else (and i can understand it in the future) $("#newZones").submit(function(ev) { ev.preventDefault() let fdata = $(this).serialize() console.log(fdata) $.ajax({ type: 'post', data: { 'ajax': 'addNewZones', 'data': fdata, }, success: function(resp) { $('.alert-container').html(resp).show() } }) }) and if (isset($_POST['ajax'])) { switch ($_POST['ajax']) { case 'addNewZones': exit(addNewZones($pdo, $_POST['data'])); break; } } Once the data has been sent into PHP function. Do i need to unserialise it to insert it? Also, if the zone name field is blank the whole row of 3 fields need to be removed from the array that is sent. Would this be a filter?
  24. Landed here: Seems to be working but i dont know if there is a more optimal way of doing this. Feels a bit like a hack. $('#addZonesButton').on('click', function() { $zNameArr = [] $zPrintArr = [] $zTypeArr = [] $data = []; $('.zoneType').each(function() { $zType = $(this).val() $zTypeArr.push($zType) }) $('.zoneName').each(function() { $zName = $(this).val() $zNameArr.push($zName) }) $('.printValue').each(function() { $pVal = $(this).val() $zPrintArr.push($pVal) }) for (i = 0; i < $zNameArr.length; i++) { $data[i] = Array($zTypeArr[i], $zNameArr[i], $zPrintArr[i]); } $filteredData = $data.filter(function(x) { /* here, x is an array, not an object */ return !(x.some(element => element === (undefined || null || ''))) }); }) The data is output as: [ [ "1", "1", "p1" ], [ "1", "3", "p3" ], [ "1", "2", "p2" ] ] Would have preferred this to have keys also. Now i need to send it to php with ajax and insert it.
×
×
  • 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.