Jump to content

Get PHP Variables with Ajax or Jquery


chinoknot
Go to solution Solved by Barand,

Recommended Posts

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?

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

  • Solution

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;
?>
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.