Submitting a form in Sencha Touch 2
I am trying to submit a form with a standard form that was specified in the downloaded SDK.
Anyone have any info on how it went? Any advice on how to pass it?
I am trying to create a very simple form to get some information.
{ title: 'Register',
iconCls: 'add',
xtype: 'formpanel',
url: 'contact.php',
layout: 'vbox',
scrollable: true,
items: [
{
xtype: 'fieldset',
title: 'Opt In',
instructions: 'Want more info on Monster Energy? Opt-in to receive updates. ',
items: [
{
xtype: 'textfield',
label: 'First Name',
name: 'fname',
},
{
xtype: 'textfield',
label: 'Last Name',
name: 'lname',
},
{
xtype: 'emailfield',
label: 'Email',
name: 'email',
},
{
xtype: 'numberfield',
label: 'Phone',
name: 'phone',
}
]
},
{
xtype: 'button',
text: 'Send',
ui: 'confirm',
handler: function() {
this.up('formpanel').submit();
}
}
]
}
contact.php
<?phpinclude_once "config.php";
mysql_connect($dbhost, $dbuser, $dbpasswd);
mysql_select_db($dbname) or die ("Cannot select db" . mysql_error());
session_start();
//***** Get today date
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");
$firstName = addslashes($_POST['fname']);
$lastName = addslashes($_POST['lname']);
$email = $_POST['email'];
$phone = $_POST['phone'];
$sql = "INSERT INTO customer_31
(mb_firstName,
mb_lastName,
mb_phone,
mb_email,
mb_date_entered)
VALUES
('$firstName',
'$lastName',
'$phone',
'$email')";
header("location:index.php");
exit();
?>
+3
source to share
2 answers
So, until I used .up with the form submission, I did a bit of something and got it to work by first using getValues () on the parent elements, doing some validation, and then including it in PHP. Here's some code hoping it helps:
handler: function(btn){
var vals = btn.parent.parent.getValues();
vals.time = new Date();
//basic email validation
var re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var result=re.test(vals.email);
if (result === true){
Ext.Ajax.request({
url: btn.parent.parent.getUrl(),
params: {
data:Ext.JSON.encode(vals),
encoded: vals.toString(),
vals: 'other='+JSON.stringify(vals)
},
success: function(res, vals){
console.log(res);
console.log(JSON.stringify(vals));
Ext.Viewport.getMasked().hide();
Ext.Msg.alert('Thank you', 'Message received successfully.', Ext.emptyFn);
Ext.getCmp('contactForm').reset();
},
failure: function(res){
console.log('form not submitted', res);
Ext.Viewport.getMasked().hide();
Ext.getCmp('contactForm').reset();
Ext.Msg.alert('Sorry something went wrong', 'Please try that again', Ext.emptyFn);
}
});
}
if (result === false){
Ext.Viewport.getMasked().hide();
Ext.Msg.alert('Invalid Email', 'Please try again', Ext.emptyFn);
Ext.getCmp('contactForm').reset();
}
}
PHP
<?php
$errorMessage = "";
$db = mysql_connect("YOURDB","USER","PASS");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db('formtest' ,$db);
$json = $_POST['data'];
$json = utf8_encode($json);
$insert = "INSERT INTO formdata VALUES ('".$json."')";
var_dump($_GET);
if(mysql_query($insert))
{
echo('values inserted successfully');
header("Location: thankyou.html");
}
else
{
echo('failure' . mysql_error());
}
exit();
?>
0
source to share