PDA

View Full Version : [Help - PHP] Posting php to xml



IFS
10-05-2009, 12:24 PM
can anyone here maybe help me with this, posting data gathered from php to an xml file please?

FlashD
10-05-2009, 12:50 PM
I'm not sure I understand what you are asking ... do you mean using PHP to generate an XML file?

Eris
10-05-2009, 01:02 PM
I'm not sure I understand what you are asking ... do you mean using PHP to generate an XML file?

If this is what you're trying to do, simply add



header("content-type: text/xml");


Somewhere in the beginning of your code (i.e. before you write any data).

Miss Moonlight
10-05-2009, 01:06 PM
There is a way to post XML data from PHP, maybe this (http://www.codediesel.com/php/posting-xml-from-php/) would help, but i'm not sure of exactly what you're trying to do.

IFS
10-05-2009, 02:29 PM
hmmm I'm not really sure my boss wants this done, he didn't really give me any details, but the conclusion I came to is he wants me to take data gatherd from a post page, a page where we gather data from clients like name, surname, id number, eamil address etc. and put that into xml format and then link it to a URL given to us by the sponsor on a remote server. I know this all sounds pretty confusing but I myslef don't really get whats going on.

FlashD
10-05-2009, 05:44 PM
There is a way to post XML data from PHP, maybe this (http://www.codediesel.com/php/posting-xml-from-php/) would help, but i'm not sure of exactly what you're trying to do.
Then this is what you need, just use the try, catch function or else curl might give you headaches with errors when the data stream won't be available.

IFS
10-06-2009, 01:16 AM
Okay I tried the code miss moonlight linked me too, but I don't quite understand how it works. I have never done XML before and I have no idea how to use it, my boss wants me to take the information from this page
https://www.ratesdirect.co.za/insurance/carinsurance/xmlcontactform.php
and then post it in a xml format that's suppose to go to this link https://www.outsurance.co.za/OLL/goleadtest.asp

FlashD
10-06-2009, 06:55 AM
I'm assuming the "xmlcontactform.php" script is the one you are building.

I'll make an example of the script for the first entry(first name):


<?php
// when you press the submit button this part is processed
if( $_REQUEST['action'] == "xml" )
{
// this is where we add the gathered data from the form below
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= "<CustomerData>\n\t<FirstName>" . $_POST['firstname'] . "</FirstName>\n</CustomerData>";

// this is a function that will save the content to a normal XML file
saveToFile($xml);
}

// this prints the form as you have it on your site (only the first name though)
print "<html>
<body>
<form action=\"$PHP_SELF?action=xml\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"text\" name=\"firstname\"><p>
<input type=\"submit\" value=\"Submit\">
</form>
</body>
</html>
";
?>


Then it's up to your client to link his "goleadtest.asp" script to your XML file.

cURL is a function when you have to gather data from another site and in your situation can't help you.

Hope this helps you a little. ^_^

IFS
10-06-2009, 07:18 AM
thanks alot man, all this is greatly appreciated


ps. I just tried the code you gave me in a test page and I got this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /usr/wwws/users/ratesg/insurance/carinsurance/test.php on line 7

I have no idea what this means.

FlashD
10-06-2009, 04:48 PM
My code doesn't work as is. I wrote it only as an example. It's missing the function saveToFile() which writes the data into a file.

The error shown is because of this line


$xml .= "<CustomerData>\n\t<FirstName>$_POST['firstname']</FirstName>\n</CustomerData>";


It should be:


$xml .= "<CustomerData>\n\t<FirstName>".$_POST['firstname']."</FirstName>\n</CustomerData>";


Just to see if the XML works use this one:


<?php
// when you press the submit button this part is processed
if( $_REQUEST['action'] == "xml" )
{
// this is where we add the gathered data from the form below
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= "<CustomerData>\n\t<FirstName>${_POST['firstname']}</FirstName>\n</CustomerData>";

// this is a function that will save the content to a normal XML file
print $xml;
die();
}

// this prints the form as you have it on your site (only the first name though)
print "<html>
<body>
<form action=\"$PHP_SELF?action=xml\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"text\" name=\"firstname\"><p>
<input type=\"submit\" value=\"Submit\">
</form>
</body>
</html>
";
?>

IFS
10-07-2009, 03:15 PM
thanks for all the help with this, I have another question, what if I create and xml file with empty fields you know just <firstname></firstname> etc. and then by clicking the continue button it adds the data to the xml file and then sends it to the link I got

FlashD
10-07-2009, 03:35 PM
You can do that, but it will just make things more complex, because you will have to write a function that is able to read your empty file. My suggestion is to hardcode the fields as I showed you and then make a saveToFile() function that reads the already created XML file (so the old data are not overwritten) and adds the new data to it.

Eris
10-07-2009, 03:50 PM
You can do that, but it will just make things more complex, because you will have to write a function that is able to read your empty file. My suggestion is to hardcode the fields as I showed you and then make a saveToFile() function that reads the already created XML file (so the old data are not overwritten) and adds the new data to it.

Sounds like a really awkward solution. In general, temp files and web programming do not go well together. Not only will you get uniqueness problems (what if two users simultaneously use the form? this is solvable, but it's gonna be ugly). A bigger problem is, what if the user doesn't hit submit? Then a temporary file will litter your server forever.

FlashD
10-07-2009, 04:40 PM
Eris has a point here. If two sessions write to the file at the same time one of the two will be lost. To avoid this, the best way is to use a database. You can use two PHP files, one that writes to the DB, while the other works as an XML builder, which gets this data from the DB and outputs it in XML format. This second script would also work as the file the second server links to.

IFS
10-08-2009, 02:05 AM
okay but only my boss will use that form, he gets info from people and manually input them into that form and from there he sends it to the link given to us from our clients (if that makes sense) so there will only be one person at a time who would use that form.


ps.

quick question how do you set the minimum value of an input type="text" field in html, I tried but my methods aren't working

FlashD
10-08-2009, 07:29 AM
In that case use my first solution. ;)

As for your other question you can't. The min value for an input element cannot be set. You can use PHP to do the trick though:



// returns the message and stops the script if the length is not above 6 characters
if( strlen($_POST['firstname']) < 6 )
{
echo "Your first name is not long enough. Go tell your government you want a name change. :P";
die();
}

Eris
10-08-2009, 12:14 PM
In that case use my first solution. ;)

As for your other question you can't. The min value for an input element cannot be set. You can use PHP to do the trick though:



// returns the message and stops the script if the length is not above 6 characters
if( strlen($_POST['firstname']) < 6 )
{
echo "Your first name is not long enough. Go tell your government you want a name change. :P";
die();
}


Of course you can, with javascript. You should still do the above in case the user doesn't have javascript enabled, or uses some weird browser, but on top of that, you can add a javascript that does client-side validation to save server processing power on requests.



<html>
<head>
<script language="javascript">
function validate_form() {
var name = document.getElementById('name');

// Silently deal with errors:
if(name == null) return true; // Go on with with submit

// Check name length
if(name.value.length < 5) {
alert("Name must be longer than 5 letters");
return false; // Do not go on with submit
}
else return true; // Go on with submit
}
</script>
</head>
<body>
<form id="myform" onSubmit="return validate_form();" method="POST" action="whatever">
<input type="text" id="name"></input>
<input type="submit"></input>
</form>
</body>
</html>


I don't have my good DOM programming book around, but I think that approach should be pretty safe and fail gracefully where it doesn't work.

IFS
10-08-2009, 12:44 PM
Thanks for all the help FlashD and Eris, and Miss Moonlight its all greatly appreciated

IFS
10-12-2009, 02:19 AM
My code doesn't work as is. I wrote it only as an example. It's missing the function saveToFile() which writes the data into a file.

The error shown is because of this line


$xml .= "<CustomerData>\n\t<FirstName>$_POST['firstname']</FirstName>\n</CustomerData>";
It should be:


$xml .= "<CustomerData>\n\t<FirstName>".$_POST['firstname']."</FirstName>\n</CustomerData>";
Just to see if the XML works use this one:


<?php
// when you press the submit button this part is processed
if( $_REQUEST['action'] == "xml" )
{
// this is where we add the gathered data from the form below
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= "<CustomerData>\n\t<FirstName>${_POST['firstname']}</FirstName>\n</CustomerData>";

// this is a function that will save the content to a normal XML file
print $xml;
die();
}

// this prints the form as you have it on your site (only the first name though)
print "<html>
<body>
<form action=\"$PHP_SELF?action=xml\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"text\" name=\"firstname\"><p>
<input type=\"submit\" value=\"Submit\">
</form>
</body>
</html>
";
?>


this is really good code, but how would I use it?

The method I used it in is as a post page, you enter info in the contactform and it gets posted automatically to the xml page, but this give me a new form where I then have to enter in data again, and then it takes me to the xml page, here is a link to see what I mean https://www.ratesdirect.co.za/insurance/carinsurance/New%20Folder/xml-gate.php how do I get the xml file it posts to as a file on my computer or get it to mail the xml page to me?

FlashD
10-12-2009, 06:37 AM
I checked your site and you just redirected your form to mine and this is not what I meant. You have to edit my form to match yours and join them.

You have to take my script and include it somewhere in your own script and change this part:


<html>
<body>
<form action=\"$PHP_SELF?action=xml\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"text\" name=\"firstname\"><p>
<input type=\"submit\" value=\"Submit\">
</form>
</body>
</html>

in my script with yours. The action attribute in the form element needs to be the same as mine though.

IFS
10-13-2009, 01:15 AM
I did change that part, and it didn't give me any display, so I changed it back before I posted the link, because I thought I might have messed something up

FlashD
10-13-2009, 05:37 AM
PM me the PHP file with the form you are using and I'll see if I can help you with it a little more. Like that my hands are pretty much tied.

IFS
10-13-2009, 12:15 PM
thanks FlashD much appreciated, just one noob question from my side, how does one upload files to a private message? I tried earlier today but I couldn't find a place where I could do that

FlashD
10-13-2009, 03:56 PM
Change the extension of your script from .php to .phps and PM me the link to it. I will look at it only after tomorrow, since I won't be home tomorrow. You can't upload files via PMs, btw.

IFS
10-15-2009, 05:13 AM
I'm sorry this must be getting annoying, but this is what need to be done

Required Fields:
-Title
-Firstname
-Lastname
-ID
-We also need at least 1 contact number.

Validation:
-No numeric or special characters to be sent through firstname or lastname.
-ID must have 13 numeric values no letters or special characters.
-Contact number must be numeric and not have special characters or spaces.
- Telephone number. The area code must be 3 digits and number must be 7.
- Mobile number must be 10 digits.
-Email must only allow @_.- to be accepted no other special characters.

XML, replace VALUES as required.
<?xml version="1.0" encoding="UTF-8"?>
<lead>
<mode>TEST</mode> keep as TEST only change if directed
<title>TITLE</title> Mr, Mrs, Miss, Dr, Prof are the only values we currently accept
<firstname>FIRSTNAME</firstname>
<lastname>LASTNAME</lastname>
<id>ID</id> Will help with validation and reducing duplicate leads
<homecode>HOMECODE</homecode>
<hometel>HOMETEL</hometel>
<workcode>WORKCODE</workcode>
<worktel>WORKTEL</worktel>
<mobile>MOBILE</mobile>
<email>EMAIL</email>
<comment>COMMENT</comment>
<source>RATESDIRECT</source> This identifies your source
<notes></notes> Leave blank
<language>E</language> E – English (default), A – Afrikaans
<product>PRODUCT</product> P – Personal (Default), B – Business
</lead>

The above XML can then be posted to https://www.outsurance.co.za/OLL/goleadtest.asp a XML response will be then be returned.
The XML should be posted in a variable called lead and the XML also needs to be URLencoded.

<?xml version="1.0" encoding="UTF-8"?>
<response>
<resultNo><resultNo> -1 if an error occurred. 0 is successful
<resultMsg></resultMsg> On success returns reference number for the lead generated.
</response>

These are the possible returned values for resultMsg


^
is what should happen or whatever. I'm really sorry for asking so much help but I have no idea what the hell I'm suppose to do here

FlashD
10-15-2009, 02:59 PM
I'm going to make you the script myself, I'll just need a few days since right now I'm rather busy with other stuff.

IFS
10-15-2009, 03:19 PM
Thanks alot man, I really appreciate this, you have no idea how much