PDA

View Full Version : Handler question with php



ajg3
18 Jun 2011, 6:37 PM
I am trying to call a php that runs when i click a button. Here is the code i currently have:



xtype: 'button',
text: 'Submit',
handler: function(){
Ext.Ajax.request({
url: 'submit.php'
});
}


And here is the php script i want run. Its real simple, just trying to get the hang of stuff.


<?php
$to = "test@test.com";
$subject = "Testing the php submit";
$body = "bah";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

Is there a special place i have to put my php script on my server? In the root directory or my js directory?

ajg3
18 Jun 2011, 8:30 PM
I figured it out, but i have another issue..


xtype: 'button',
text: 'Submit',
handler: function(){
Ext.Ajax.request({
url: 'submit.php',
method: 'POST'
});
}

How can i include values from a form into the php script?



items: [{
xtype: 'textfield',
name : 'fname',
label: 'First Name',
required: true
},{
xtype: 'textfield',
name : 'lname',
label: 'Last Name',
required: true
}]


These are some of the fields. How do i get fields from my form to be sent in my php script?

I tried this:

params: {
firstname : fname,
lastname : lname
}

and in my php script i added:

$body = firstname;

but it doesn't work.

tolbahady
18 Jun 2011, 9:31 PM
You need to get a reference to the textfields, use the id or itemId atribute


items: [{
xtype: 'textfield', id:'fname',
name : 'fname',
label: 'First Name',
required: true
},{
xtype: 'textfield', itemId:'lname',
name : 'lname',
label: 'Last Name',
required: true
}]


Then use Ext.getCmp, panel.getComponent, or panel.query('#id') passing the id or itemId, and call getValue()



params: {
firstname : Ext.getCmp('fname').getValue(),
lastname : this.getComponent('lname') //you need to set the scope in order to use this
}


At the server side you can use $_POST or $_GET depending on the way you sent the data



$firstname = $_POST['fname'];
$lastname = $_POST['lname'];

razzle
19 Jun 2011, 12:54 PM
this is how i am creating the panel.


var Autocard = new Ext.Panel( {

*code

})


and i have this for my handler function:


handler: function(){

console.log(Ext.getCmp('fname').getValue())

Ext.Ajax.request({
url: 'submit.php',
method: 'POST',
scope: this,
params: {
firstname : Ext.getCmp('fname').getValue()
}


});
}

The console shows what ever i have typed in the field. But the php is sending a blank body. This is what i have in my script now.


<?php
$to = "test@test.com";
$subject = "test";
$firstname = $_POST['fname'];
mail($to, $subject, $firstname)
?>

I feel like it is a simple change but i have tried alot of things and still nothing. Any ideas?