-
1. Re: How do I post content from input field into PHP file?
Chepe Nicoli Apr 29, 2015 8:46 AM (in response to M123456)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 May 20, 2015 5:31 AM (in response to Chepe Nicoli)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 May 20, 2015 8:32 AM (in response to M123456)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 May 20, 2015 9:27 AM (in response to Chepe Nicoli)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 May 20, 2015 9:39 AM (in response to M123456)WHere and how are you getting this error?
-
6. Re: How do I post content from input field into PHP file?
M123456 May 20, 2015 10:06 AM (in response to Chepe Nicoli)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 May 20, 2015 10:17 AM (in response to M123456)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 May 20, 2015 12:59 PM (in response to Chepe Nicoli)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 Chepe Nicoli for giving the script!