Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

not able to pass form data to php

Avatar

Level 1

I am using AJAX in Jquery to pass form field variables to php and using php i want those data should get saved in a XML file in a AEM bin or clientlib.

I am not able to pass those data to PHP using ajax .this is what i am doing:

 

$('#submit').click(function() {

         var n1=$('#Name').val();
            var em1=$('#Email').val();
            var org1=$('#Org').val();
            var ph1=$('#phone').val();
             var c1=$('#Country').val();
         var ic1=$('#Inquiry_Contact').val();  


    $.ajax({
    type: "POST",
    url: "/etc/designs/team-a/clientlibs/scripts/dummy.php",
    dataType: "xml",
    contentType: "application/xml",
    data:"{'Name':'" + n1 + "','email':'" + em1 + "','organisation':'" + org1 + ",'phone':"+ph1+",'country':"+c1+",'inquiry-contact':"+ic1+"}", 
    success: function (res) {
        alert("thank you"+n1+" we will contact you soon");
    },
    error: function (res) {
        alert("XML: not working! " + res.statusText);
    }
    });

 

 

And this is my php file:

<?php
$data=$_REQUEST['data']
    

$n1 = $_POST['n1'];
$em1 = $_POST['em1'];
$org1 = $_POST['org1'];
$ph1 = $_POST['ph1'];
$c1 = $_POST['c1'];
$ic1 = $_POST['ic1'];

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('data.xml');

$element = $xml->getElementsByTagName('reports')->item(0);

$ic1 = $element->getElementsByTagName('ic1')->item(0);
$n1 = $element->getElementsByTagName('n1')->item(0);
$em1 = $element->getElementsByTagName('em1')->item(0);
$org1 = $element->getElementsByTagName('org1')->item(0);
$ph1 = $element->getElementsByTagName('ph1')->item(0);
$c1 = $element->getElementsByTagName('c1')->item(0);

$newItem = $xml->createElement('reports');


$newItem->appendChild($xml->createElement('ic1', $_POST['ic1']));
$newItem->appendChild($xml->createElement('n1', $_POST['n1']));
$newItem->appendChild($xml->createElement('em1', $_POST['em1']));
$newItem->appendChild($xml->createElement('org1', $_POST['org1']));
$newItem->appendChild($xml->createElement('ph1', $_POST['ph1']));
$newItem->appendChild($xml->createElement('c1', $_POST['c1']));


$xml->getElementsByTagName('reports')->item(0)->appendChild($newItem);

$xml->save('data.xml');

echo "Data has been written.";

?>

BUT its not working....i thing this php is not called ...their is some issue in ajax only.

Looking forward for a help

thanks in advance:

1 Accepted Solution

Avatar

Correct answer by
Level 10

We can pass data to a JSP using AJAX - there is no issue see: 

https://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

See: 

$('#submitget').click(function() {
    var failure = function(err) {
         alert("Unable to retrive data "+err);
     };
           
//Get the user-defined values to persist in the database
var filter=   $('#custQuery').val() ; 
        
var url = location.pathname.replace(".html", "/_jcr_content.query.json") + "?filter="+ filter;
        
$.ajax(url, {

        dataType: "text",
        success: function(rawData, status, xhr) {
            var data;
            try {
                data = $.parseJSON(rawData);

I have never had an issue with JSP - however - never used PHP and AEM. 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

We can pass data to a JSP using AJAX - there is no issue see: 

https://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

See: 

$('#submitget').click(function() {
    var failure = function(err) {
         alert("Unable to retrive data "+err);
     };
           
//Get the user-defined values to persist in the database
var filter=   $('#custQuery').val() ; 
        
var url = location.pathname.replace(".html", "/_jcr_content.query.json") + "?filter="+ filter;
        
$.ajax(url, {

        dataType: "text",
        success: function(rawData, status, xhr) {
            var data;
            try {
                data = $.parseJSON(rawData);

I have never had an issue with JSP - however - never used PHP and AEM.