8 Replies Latest reply on May 20, 2015 12:59 PM by M123456

    How do I post content from input field into PHP file?

    M123456

      I made an input field in Adobe Edge and I know how to reuse the content whitin Adobe Edge but I want to reuse it from within a PHP file so I can store the content in a MySQL database (I know how to store data in a database).

      So my question: does anybody know how to post the content from the input field into a PHP file?

      Thanks in advance.

        • 1. Re: How do I post content from input field into PHP file?
          Chepe Nicoli Level 1

          You can use AJAX, on your click method:

           

          $.ajax({

                  url: "test.php", // your php file

                  type: "post", // get or post

                  data: data: {status: "something", yourInput: input}, // your data, here you add whatever you want to send

                  success: function(){ // you can use a function to see the results

                      alert("success");

                      $("#result").html('Submitted successfully');

                  },

              });

          • 2. Re: How do I post content from input field into PHP file?
            M123456 Level 1

            Thanks for your response.

            I can't get it to work.

             

            Your code will send the data to PHP right?

            From within the PHP file I want to store the data in my database. I know how that works when I normally make a html/php webpage but not when this is made in Adobe Edge.

            How do I use the data sent with ajax in my PHP file?

             

            Thanks for helping me out!

            • 3. Re: How do I post content from input field into PHP file?
              Chepe Nicoli Level 1

              Try this code:

               

              var formA = '<form id="formA" action="http://www.your.php" method="post" enctype="multipart/form-data">';

              formA += '<input id="SomeData" name="SomeData" type="text" >';

              formA += '<input type="hidden" name="someOtherData" value="someOtherData" />';

              formA += '<input type="submit" value="toDB" id="toDB" /></form>';

              sym.$("formAInput").html(formA);

              var frm = $('#formA');

              frm.submit(function (ev) {

                   $.ajax({

                        dataType: 'text',

                        type: frm.attr('method'),

                        url: frm.attr('action'),

                        data: new FormData(this),

                        success:function(result){

                             alert("Yes!!!!");

                        },

                     error: function(result){

                          alert("%$#$%");

                     }

                        });

                     ev.preventDefault();

              });

               

              Create a TEXT div in your EA, in this example give it the name of "formAInput".

              When executed your DIV "formAInput" will be replaced with a form.

               

              The ajax function will prevent the form to be submitted the normal way and will send the data to your php file

               

              In your.php you could do something like this:

               

              if ($_POST['toDB']) {

                $dataA = $_POST['SomeData'];

                $dataB = $_POST['someOtherData'];

                // Do DB stuff here

              }

               

              Hope this helps!

              • 4. Re: How do I post content from input field into PHP file?
                M123456 Level 1

                Thanks you for helping me out!

                 

                I think your code is really close to what it should be but I am getting an error message:

                "Notice: Undefined index: toDB"

                 

                Any ideas why?

                • 5. Re: How do I post content from input field into PHP file?
                  Chepe Nicoli Level 1

                  WHere and how are you getting this  error?

                  • 6. Re: How do I post content from input field into PHP file?
                    M123456 Level 1

                    In the PHP file.

                     

                    When I am using this part outside the if stament it works!

                    $dataA = $_POST['SomeData'];

                      $dataB = $_POST['someOtherData'];

                      // Do DB stuff here

                     

                    So this part gives the error:

                    if ($_POST['toDB']) {}

                    • 7. Re: How do I post content from input field into PHP file?
                      Chepe Nicoli Level 1

                      That 'IF' is used only to verify the call comes from you (from EA), if this is causing problems don not use it.

                       

                      Cheers!

                      • 8. Re: How do I post content from input field into PHP file?
                        M123456 Level 1

                        I think I figured it out. Now I use:

                         

                        if(isset($_POST['toDB'])){

                         

                        And if someone else wants to use this code, the ev.preventDefault(); should be used in the beginning of the script. Otherwise it will redirect you to the php.

                        So in the end the working script wil look like this:

                        ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------ -----

                        var formA = '<form id="formA" action="http://www.your.php" method="post" enctype="multipart/form-data">';

                        formA += '<input id="SomeData" name="SomeData" type="text" >';

                        formA += '<input type="hidden" name="someOtherData" value="someOtherData" />';

                        formA += '<input type="submit" value="toDB" id="toDB" /></form>';

                        sym.$("formAInput").html(formA);

                        var frm = $('#formA');

                        frm.submit(function (ev) {


                        ev.preventDefault();


                             $.ajax({

                                  dataType: 'text',

                                  type: frm.attr('method'),

                                  url: frm.attr('action'),

                                  data: new FormData(this),

                                  success:function(result){

                                       alert("Yes!!!!");

                                  },

                               error: function(result){

                                    alert("%$#$%");

                               }

                                  });

                        });

                        ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------ -----

                        Create a TEXT div in your EA, in this example give it the name of "formAInput".

                        When executed your DIV "formAInput" will be replaced with a form.

                         

                        The ajax function will prevent the form to be submitted the normal way and will send the data to your php file

                         

                        In your.php you could do something like this:

                         

                        ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------ -----

                        if(isset($_POST['toDB'])){

                          $dataA = $_POST['SomeData'];

                          $dataB = $_POST['someOtherData'];

                          // Do DB stuff here

                        }

                        ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------ -----

                         

                        Thanks for giving the script!