Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. function add_product($form, &$form_state) {
  2. //Several other fields but the main one around this issue is below.
  3. $form['img_mng'] = array(
  4. 'label' => array(
  5. '#type' => 'markup',
  6. '#markup' => '<h3>Upload Images</h3>',
  7. ),
  8. 'file1' => array(
  9. '#type' => 'file',
  10. '#size' => 22,
  11. ),
  12. 'addMore' => array(
  13. '#type' => 'button',
  14. '#value' => t('Add More Images'),
  15. '#ajax' => array(
  16. 'callback' => 'add_img_upload',
  17. 'method' => 'append',
  18. 'wrapper' => 'kc_img_uploads',
  19. 'keypress' => 'true',
  20.  
  21. ),
  22. '#prefix' => '<div id="kc_img_uploads">&nbsp;</div>',
  23. ),
  24. );
  25.  
  26. $form['submit'] = array(
  27. '#type' => 'submit',
  28. '#value' => t('Add Product'),
  29. );
  30. return $form;
  31. }
  32.  
  33. function add_product_validate($form,&$form_state) {
  34. //Appropriate Validation
  35. dpm($_POST); //$_POST does not show the fields, not sure how that is even possible since the new fields are added to the form.
  36. dpm(get_defined_vars()); //None of the variables show the new fields.
  37. }
  38.  
  39. function add_product_submit($form,&$form_state) {
  40. // Appropriate database actions....
  41. dpm($form_state); //No values for new fields.
  42. dpm($_POST); //New fields do not even show in the $_POST.
  43. }
  44.  
  45. function add_img_upload($form,&$form_state) {
  46. dpm($form_state); //Does not show the added field below if the button is clicked more than once.
  47. $num_of_files = 1;
  48. foreach ( $form['img_mng'] as $key => $value ) {
  49. if ( preg_match('/^file[0-9]+$/',$key) )
  50. $num_of_files++;
  51. }
  52. $form['img_mng'][sprintf('file%d',$num_of_files)] = array(
  53. '#type' => 'file',
  54. '#size' => 22,
  55. '#id' => sprintf('edit_file%d',$num_of_files),
  56. '#name' => sprintf('files[file%d]',$num_of_files),
  57. '#parents' => sprintf('file%d',$num_of_files),
  58. '#array_parents' => sprintf('file%d',$num_of_files),
  59. );
  60. $form_state['values'][sprintf('file%d',$num_of_files)] = '';
  61. $form_state['file2'] = NULL;
  62. return $form['img_mng'][sprintf('file%d',$num_of_files)]; //The fields are added to the form every time the button is clicked. However, the id and name of the elements are only incremented once. ie file1 becomes file2 but never makes it to file3+. So I will have one file1 and 1 to many file2's.
  63. }
  64.  
  65. function add_img_upload($form,&$form_state) {
  66. return $form['img_mng'];
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement