chinoknot Posted January 12, 2016 Share Posted January 12, 2016 Hello,I can't get all the variables contained in a form, a form which I fill. I saw many tutorials, but with no luck. I need to get this variables and pass to another PHP page.Can you help me? $(document).ready(function() { $('#BtnInserisci').click(function(){ var TxtData = $("#TxtData").val(); var TxtOraAppuntamento = $("#TxtOraAppuntamento").val(); var TxtNome = $("#TxtNome").val(); var TxtCognome = $("#TxtCognome").val(); var TxtCfIva = $("#TxtCfIva").val(); var TxtRecapitoPri = $("#TxtRecapitoPri").val(); var TxtRecapitoMob = $("#TxtRecapitoMob").val(); var TxtRecapitoAlt = $("#TxtRecapitoAlt").val(); var TxtIndirizzo = $("#TxtIndirizzo").val(); var TxtComune = $("#TxtComune").val(); var cap = $("#cap :selected").text(); var TxtDettInt = $("#TxtDettInt").val(); var TxtTipoInt = $("#TxtTipoInt").val(); var TxtIndMaps = $("#TxtIndMaps").val(); var TxtNoteIntervento = $("#TxtNoteIntervento").val(); var TxtNoteIndirizzo = $("#TxtNoteIndirizzo").val(); /*alert(TxtData); alert(TxtOraAppuntamento); alert(TxtNome); alert(TxtCognome); alert(TxtCfIva); alert(TxtRecapitoPri); alert(TxtRecapitoMob); alert(TxtRecapitoAlt); alert(TxtIndirizzo); alert(TxtComune); alert(cap); alert(TxtDettInt); alert(TxtTipoInt); alert(TxtIndMaps); alert(TxtNoteIntervento); alert(TxtNoteIndirizzo); $(this)({ url: 'test.php', type: 'POST', dataType: 'html', data: $('#BtnInserisci'), success: function(html) { $('#BtnInserisci').before(html); } }); }); }); The BtnInserisci is the button I press for retrieve my variables.The alert is working fine.Where is my mistake? Quote Link to comment https://forums.phpfreaks.com/topic/300286-get-php-variables-with-ajax-or-jquery/ Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 I'm not sure what tutorails you have been looking up, but this line: $(this)({ is wrong. use of $(this) in jquery references the current object, in this case your button, it's not an ajax call. you need to change it to $.ajax({ Also, when using jquery to bind events to existing DOM elements you should use "on" to do it. $('#BtnInserisci').on('click', function(){ ... see :here: Quote Link to comment https://forums.phpfreaks.com/topic/300286-get-php-variables-with-ajax-or-jquery/#findComment-1529495 Share on other sites More sharing options...
Solution Barand Posted January 12, 2016 Solution Share Posted January 12, 2016 Easiest way to get the form fields is to use serialize(). Try this example testing.html <html> <head> <title>Example</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type='text/javascript'> $().ready(function() { $('#BtnInserisci').click(function() { var formdata = $("#form1").serialize(); $.post( "testing.php", formdata, function(html) { $('#BtnInserisci').before(html); }, 'html' ) }) }) </script> </head> <body> <form id='form1'> Field 1: <input type='text' name='field1' value='aaaa'><br> Field 2: <input type='text' name='field2' value='bbbb'><br> Field 3: <input type='text' name='field3' value='cccc'><br> <input type='button' name='BtnInserisci' id='BtnInserisci' value='Test'> </form> </body> </html> testing.php <?php $html = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { $html = '<div style="width: 200px; border: 1px solid gray; margin:10px;padding:5px">'; foreach ($_POST as $k => $v) { $html .= "<p>$k : $v</p>"; } $html .= "</div>\n"; } echo $html; ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/300286-get-php-variables-with-ajax-or-jquery/#findComment-1529498 Share on other sites More sharing options...
chinoknot Posted January 26, 2016 Author Share Posted January 26, 2016 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/300286-get-php-variables-with-ajax-or-jquery/#findComment-1530444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.