How do i passed data into modal using ajax codeigniter - php

Im trying to solve this problem for few hours already im trying to fetch a data from database into modal to update it but im having difficulty in fetching the data when i click the button that will trigger to display the modal it doesnt show i tried many things already this is my code
Script
function show_profile(){
$.ajax({
type : 'ajax',
url : '<?php echo site_url('pages/profile_data')?>',
async : false,
dataType : 'json',
success : function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>' +
'<th>'+'Faculty Name : '+ '</th>' + '<td>' + data[i].faculty_name+'</td>'+ '</tr>' + '<tr>' +
'<th>'+'Faculty Type : '+ '</th>' + '<td>' + data[i].faculty_type+'</td>'+ '</tr>' + '<tr>' +
'<th>'+'Email Address : '+ '</th>' + '<td>' + data[i].email_address+'</td>'+ '</tr>' + '<tr>' +
'<th>'+'Department Name: '+ '</th>' + '<td>' + data[i].department_name+'</td>'+ '</tr>' + '<tr>'+
'Edit'+' '+
'<th>'+'Update' + '</th>' + '</tr>';
}
$('#show_data').html(html);
}
});
}
$('#show_data').on('click','.item_update',function(){
var faculty_name = $(this).data('faculty_name');
var faculty_type = $(this).data('faculty_type');
var email_address = $(this).data('email_address');
var department_name = $(this).data('department_name');
$('#Modal_update_info').modal('show');
$('[name="faculty_name_edit"]').val(faculty_name);
$('[name="faculty_type_edit"]').val(faculty_type);
$('[name="email_address_edit"]').val(email_address);
$('[name="department_name_edit"]').val(department_name);
});
$('#btn_update').on('click',function(){
var faculty_name = $('#faculty_name_edit').val();
var faculty_type = $('#faculty_type_edit').val();
var department_name = $('#department_name_edit').val();
var email_address = $('#email_address_edit').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('pages/update_profile')?>",
dataType : "JSON",
data : {faculty_name:faculty_name, faculty_type:faculty_type, department_name:department_name, email_address:email_address },
success: function(data){
$('[name="faculty_name_edit"]').val("");
$('[name="faculty_type_edit"]').val("");
$('[name="department_name_edit"]').val("");
$('[name="email_address_edit"]').val("");
$('#Modal_update_info').modal('hide');
show_profile();
}
});
return false;
});
Model
public function profile_list()
{
$faculty_name = $this->session->userdata('faculty_name');
$query = $this->db->select('*')
->from('faculty')
->where('faculty_name', $faculty_name)
->join('department','department.department_id = faculty.department_id')
->get();
return $query->result();
}
function update_profiledata(){
$faculty_name = $this->input->post('faculty_name');
$faculty_type = $this->input->post('faculty_type');
$department_name = $this->input->post('department_name');
$email_address = $this->input->post('email_address');
$this->db->where('faculty_name', $faculty_name);
$result = $this->db->update('faculty');
return $result;
}
Controller
public function profile_data()
{
$data = $this->page_model->profile_list();
echo json_encode($data);
}
public function update_profile()
{
$data = $this->page_model->update_profiledata();
echo json_encode($data);
}
View
<div class="card-header p-3 mb-2 bg-dark text-white text-align:center">
My Profile
</div>
<div class="card-body">
<table class="table table-striped" id="mydata">
<tbody id="show_data" style="text-align:left">
</tbody>
</table> </div>
<form>
<div class="modal fade" id="Modal_update_info" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update User Information</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-md-6 col-form-label"><b>Faculty Name</b></label>
<div class="col-md-6">
<input type="text" name="faculty_name_edit" placeholder="faculty_name" id="faculty_name_edit" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-md-6 col-form-label"><b>Faculty Type</b></label>
<div class="col-md-6">
<input type="text" name="faculty_type_edit" id="faculty_type_edit" class="form-control" placeholder="faculty_type" readonly>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-md-6 col-form-label"><b>Department</b></label>
<div class="col-md-6">
<input type="text" name="department_name_edit" id="department_name_edit" class="form-control" placeholder="department_name" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-6 col-form-label"><b>Email</b></label>
<div class="col-md-6">
<input type="text" name="email_address_edit" id="email_address_edit" class="form-control" placeholder="email_address" required="email ">
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_edit" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
</form>

Related

Getting id from MySQL to modal and passing it through Ajax

I fetched the id from the database and passed it to the modal, and then echo it on another page through ajax but the problem is that it keeps returning the same id
<?php if(mysqli_num_rows($select_invoice_query)>0) {
while($invoice_result = mysqli_fetch_assoc($select_invoice_query)) { ?>
<tr>
<td><i class="fa fa-envelope"></i></td>
</tr>
<?php }} ?>
Bootstrap Modal
<div id="send-invoice-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Send Invoice</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="email" id="invoice_to" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" id="invoice_subject" class="form-control" placeholder="Subject">
</div>
<div class="form-group">
<textarea class="form-control" id="invoice_message" rows="3" placeholder="Message"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" id="send_invoice_btn">Send</button>
<div class="error_message"></div>
</div>
</div>
</div>
</div>
JS
$('#send_invoice_btn').on('click',function(){
var invoice_id = $('#send_invoice').data('invoiceid'); // this is collecting the id from the anchor tag
var invoice_to = $('#invoice_to').val();
var invoice_subject = $('#invoice_subject').val();
var invoice_message = $('#invoice_message').val();
$.ajax({
url: "includes/send-invoice.inc.php"+invoice_id,
type: 'POST',
data: {invoice_id:invoice_id,invoice_to:invoice_to,invoice_subject:invoice_subject,invoice_message:invoice_message},
success: function(send_invoice_result){
$('.error_message').html(send_invoice_result);
}
})
});
send-invoice.inc.php File
<?php
include 'db-connection.php';
echo $invoice_id = $_GET['invoice_id'];
?>
it keeps returning the same id for some unknown reason. Can anyone tell me what am I doing wrong?
This works for me
$('.send_invoice').on('click',function(){
var invoice_id = $(this).attr('data-invoiceid');
$('#send_invoice_btn').on('click',function(){
var invoice_to = $('#invoice_to').val();
var invoice_subject = $('#invoice_subject').val();
var invoice_message = $('#invoice_message').val();
$.ajax({
url: 'includes/send-invoice.inc.php?invoice_id='+invoice_id,
type: 'POST',
data: {invoice_to:invoice_to,invoice_subject:invoice_subject,invoice_message:invoice_message},
success: function(send_invoice_result){
}
});
});
});

How to show flash message instead of alert box in popup from?

My code displays an alert box in my popup form but I want to display the flash message in my popup form:
My Ajax:
$('#password_change_form').submit(function(e) {
e.preventDefault();
var saveThis = this;
$.ajax({
type: "POST",
url: "/changepassword",
data: $(saveThis).serialize(),
success: function(data) {
$('#password_change_form')[0].reset();
alert(data);
},
});
}),
My Controller:
$current_password = $user->password;
if(md5($request_data['password']) == $current_password) {
$user_id = $user->id;
$obj_user = User::find($user_id);
$obj_user->password = md5($request_data['new_password']);
$obj_user->save();
$success_output = '<div class="alert alert-success">Password changed</div>';
} else {
$errors_output = '<div class="alert alert-danger">Wrong old password</div>';
$error = array(
'error' => $errors_output
);
echo json_encode($error);
}
$success = array(
'success' => $success_output
);
echo json_encode($success);
Here I want to show the success message in my popup form:
<div class="modal fade common_modal" id="change_password" tabindex="-1"
role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3>Změna hesla</h3>
</div>
<div class="modal-body">
<form role="form" id="password_change_form"
class="common_form_style popup_form">
<div class="row">
<div class="col-md-8 col-md-offset-2">
{{ csrf_field() }}
<span id="form_output"></span>
<div class="form-group">
<label for="password" style="width:100%">Původní heslo </label>
<input id="password" type="password" class="form-control" name="password">
<span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<label for="new_password" style="width:100%">NovÄ› heslo</label>
<input id="new_password" type="password" class="form-control" name="new_password">
<span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
<span class="help-block" style="color:#737373;font-size:14px;float:right;margin-right: 30px;font-weight: 100 !important;">MinimálnÄ› 8 znaků, jedno velké a malé písmeno a Äíslo</span>
</div>
<div class="form-group">
<label for="heslo znovu">Potvrzení heslo</label>
<input id="password_confirmation" type="password" class="form-control" name="password_confirmation">
<span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
</div>
<div class="submit-btn text-center">
<input type="submit"class="btn btn-default chci" value="Uložit" id="submit_form">
</div>
<div style="margin-top:10px;" id="success-messages"></div>
</div>
<div class="col-md-12 pull-right"></div>
</div>
</form>
I Want to display a success message but currently there is alert in my popup form. The alert screen shot:
Please change to this js code.
$(document).ready(function() {
$('#form').submit(function(e) {
e.preventDefault();
var saveThis = $(this);
$.ajax({
type: "POST",
url: "./index.php",
data: $(saveThis).serialize(),
success: function(data) {
var obj = $.parseJSON(data);
// I don't know where you want to show messages.
// In this case, outputting to id="form_output" element.
$('#form_output').html(obj.success);
}
});
});
});

ajax return data using bootstrap modal can't get parameter inside modal

I want to submit a data parameter using PHP Codeigniter that comes from ajax return data, but when I submit the form, I can't get this parameter.
HTML :
<a href="#myModalBiaya" class="trash" role="button" data-toggle="modal">
<button class="btn btn-success btn-sm" name="pdetailbiaya" value="Detail Biaya" >Detail Biaya</button>
</a>
JS :
$('#myModalBiaya').on('shown.bs.modal', function(e) {
po_id = $('#po_id').val();
$.ajax({
type: 'post',
url: '<?php echo base_url();?>admin/po_trans_receive_po/getBiaya',
data:'po_id='+po_id,
success: function(data){
$("#myModalBiaya .tdata").html(data);
}
});
});
Modal :
...
<div class="modal-body">
<div class="panel panel-default" >
<div class="panel-heading">
<div class="form-horizontal tdata" >
<div class="form-group" >
<label class='col-md-3 control-label' for='menu_name'>Biaya PPN </label>
<div class='col-md-8'>
<input type="text" name="po_biaya_ppn" id="po_biaya_ppn" readonly onkeyup="getTotalBiaya()"class="form-control text-right" value="0">
</div>
</div>
...
PHP :
function getBiaya()
{
$po_id = $this->input->post('po_id');
$resultPO = $this->md_po_trans_po->getID($po_id);
if ($resultPO) {
$resultotal = $resultPO->po_biaya_ppn+$resultPO->po_ongkos_angkut+$resultPO->po_biaya_asuransi+$resultPO->po_biaya_materai;
echo ' <div class="form-group" > ';
echo ' <label class="col-md-3 control-label">Biaya PPN </label> ';
echo ' <div class="col-md-8"> ';
echo ' <input type="text" name="po_biaya_ppn" id="po_biaya_ppn" readonly onkeyup="getTotalBiaya()" class="form-control text-right" value="'.$resultPO->po_biaya_ppn.'"> ';
echo ' </div> ';
echo ' </div> ';
}
Everything works unless the input parameter po_biaya_ppn is blank when I submit this form. When I print_r($this->input->post('po_biaya_ppn'));die(); this is returned blank.
Regards,
Thank You
Replace
data:'po_id='+po_id,
with
var form_data = {
po_id : po_id,
};
data:form_data,

Delete selected Items with modal confirmation PHP

I have this index.php code:
<form role="form" id="submit_form" class="form-horizontal">
<div class="panel-body">
<div class="form-group has-feedback">
<div class="col-sm-2">
<label class="control-label">País</label>
</div>
<div class="col-sm-9">
<?
$array_pais = array('Selecione...', 'Alemanha', 'Angola', 'Argentina', 'Bolívia', 'Brasil', 'Camarões', 'Canadá', 'Chile', 'China', 'Colombia',
'Costa Rica', 'Cuba', 'Dinamarca', 'Equador', 'Espanha', 'Estados Unidos', 'França', 'Holanda', 'Inglaterra', 'Itália', 'Japão',
'México', 'Nigéria', 'Panamá', 'Paraguai', 'Peru', 'Porto Rico', 'Portugal', 'Rússia', 'Senegal', 'Taiwan', 'Uruguai', 'Venezuela'
);
echo '<select class="form-control" name="pais" id="pais">';
foreach ($array_pais as $valor) {
echo '<option>' . $valor . '</option>';
}
echo '</select>';
?>
</div>
</div>
<div class="form-group has-feedback">
<div class="col-sm-2">
<label class="control-label">Nome:</label>
</div>
<div class="col-sm-10">
<input id="nome" name="nome" class="form-control" type="text" placeholder="Digite o nome">
</div>
</div>
<div class="form-group has-feedback">
<div class="col-sm-2">
<label class="control-label">Empresa</label>
</div>
<div class="col-sm-10">
<input id="empresa" name="empresa" class="form-control" type="text" placeholder="Digite a empresa">
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-sm-offset">
<div style="float:left; padding-right:30px">
<button type="submit" class="btn btn-lg btn-primary" aria-label="Left Align">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Adicionar
</button>
</div>
<div style="float:right;">
<button class="btn btn-lg btn-warning" onClick="parent.location='exibir.php'">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="jumbotron">
<table data-toggle="table" data-cache="false" class="table" id="list">
<thead align="center">
<tr bgcolor="#FFFFFF">
<th>PAÍS</th>
<th>NOME</th>
<th>EMPRESA</th>
<th>AÇÕES</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="inserir_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">
<span class="glyphicon glyphicon-file"></span> Inserir
</h3>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="editar_modal" role="dialog">
<div class="modal-dialog">
<form id="edit_form" class="form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">
<span class="glyphicon glyphicon-pencil"></span> Editar
</h3>
</div>
<div class="modal-body">
<div class="form-group">
<label class="control-label">País:</label>
<input id="pais_input" name="nome" class="form-control" type="text" placeholder="Digite o pais">
</div>
<div class="form-group">
<label class="control-label">Nome:</label>
<input id="nome_input" name="nome" class="form-control" type="text" placeholder="Digite a empresa">
</div>
<div class="form-group">
<label class="control-label">Empresa:</label>
<input id="empresa_input" name="empresa" class="form-control" type="text" placeholder="Digite a empresa">
</div>
<input id="id_input" type="hidden" name="id">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" aria-label="Left Align">
<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Gravar
</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</form>
</div>
</div>
<div class="modal fade" id="deletar_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">
<span class="glyphicon glyphicon-trash"></span> Exclusão
</h3>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<script>
function update_list() {
$.getJSON("get_list.php", function (data) {
if (data.response) {
$("#list").find("tbody").empty();
data.results.forEach(function (i) {
$("#list").find("tbody").append(
"<tr>" +
"<td>" + i.pais + "</td>" +
"<td>" + i.nome + "</td>" +
"<td>" + i.empresa + "</td>" +
"<td align='center'><a class='btn btn-primary glyphicon glyphicon-pencil' title='Editar' id='edit_link' href='" + JSON.stringify(i) + "'></a> | <a class='btn btn-danger glyphicon glyphicon-trash' title='Deletar' id='delete_link' href='" + JSON.stringify(i) + "'></a></td>" +
"</tr>"
);
});
}
});
}
$("#list").delegate('#edit_link', 'click', function (e) {
e.preventDefault();
var info = JSON.parse($(this).attr("href"));
var $modal = $("#editar_modal");
$modal.find("#pais_input").val(info.pais);
$modal.find("#nome_input").val(info.nome);
$modal.find("#empresa_input").val(info.empresa);
$modal.find("#id_input").val(info.id);
$modal.modal('show');
});
update_list();
$('#submit_form').submit(function () {
$.ajax({
url: "inserir.php",
type: "POST",
data: $(this).serialize(),
dataType: "json",
success: function (data) {
if (data.response) {
var $modal = $('#inserir_modal');
$modal.find(".modal-body").html(data.message);
$modal.modal('show');
update_list();
} else {
alert(data.message);
}
}
});
return false;
});
$('#edit_form').submit(function () {
$.ajax({
url: "editar.php",
type: "POST",
data: $(this).serialize(),
dataType: "json",
success: function (data) {
if (data.response) {
var $modal = $('#inserir_modal');
$("#editar_modal").modal('hide');
$modal.find(".modal-body").html(data.message);
$modal.modal('show');
update_list();
} else {
alert(data.message);
}
}
});
return false;
});
</script>
edit.php
<?php
require('conexao.php');
$id = $_POST['id'];
$pais = $_POST['pais'];
$nome = $_POST['nome'];
$empresa = $_POST['empresa'];
$query = "UPDATE tb_visitas SET nome = '$nome', empresa = '$empresa', pais= '$pais' WHERE id = $id ";
$update = mysql_query($query);
if ($update) {
$res['response'] = true;
$res['message'] = "Registro atualizado com sucesso!";
} else {
$res['response'] = false;
$res['message'] = "Erro: " . $query . "<br>" . mysql_error();
}
echo json_encode($res);
?>
get_list.php
<?php
require('conexao.php');
$sql = "SELECT id, pais, nome, empresa FROM tb_visitas";
$table = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($table) > 0) {
$res['response'] = true;
while($row = mysql_fetch_assoc($table)) {
$res['results'][] = $row;
}
} else {
$res['response'] = false;
$res['message'] = "Erro: " . $sql . "<br>" . mysql_error($con);
}
echo json_encode($res);
?>
deletar.php
<?php
require('conexao.php');
$id = $_POST['id'];
$query = "DELETE FROM tb_visitas WHERE id = $id ";
if (mysql_query($query)) {
$res['response'] = true;
$res['message'] = "Registro deletado com sucesso!";
} else {
$res['response'] = false;
$res['message'] = "Erro: " . $query . "<br>" . mysql_error($con);
}
echo json_encode($res);
?>
and conexao.php
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$hostname = 'localhost';
$username = 'root';
$senha = '';
$banco = 'visitas';
$db = mysql_connect($hostname, $username, $senha);
mysql_set_charset('latin1',$db);
mysql_select_db($banco, $db) or die ("Não foi possível conectar ao banco MySQL");
?>
So, now I'd like to put a checkbox in each item for being able to select several records and delete them at once. Another question, when I edit a record if I put a text the field it's filled, but if I put a select it doesn't work. The idea is the select field has all options of countries and letting that item from the record already selected. I hope you understand what I need. Thanks.
From what I understand, you have two questions:
How to put checkboxes on your CRUD list in order to delete all items at once.
It seems that you are using update_list() to load all items on your table. Since thats the case, you need to add an extra <td> in that row to that iteration with the checkbox element.
data.results.forEach(function (i) {
$("#list").find("tbody").append(
"<tr>" +
"<td><input class='item_checkbox' type='checkbox'/></td>" +
"<td>" + i.pais + "</td>" +
"<td>" + i.nome + "</td>" +
"<td>" + i.empresa + "</td>" +
"<td align='center'><a class='btn btn-primary glyphicon glyphicon-pencil' title='Editar' id='edit_link' href='" + JSON.stringify(i) + "'></a> | <a class='btn btn-danger glyphicon glyphicon-trash' title='Deletar' id='delete_link' href='" + JSON.stringify(i) + "'></a></td>" +
"</tr>"
);
});
Once you have that set, you can then use jquery to collect all checkboxes by class .item_checkbox and run a delete function.
How to make sure the SELECT country field is pre-selected when you EDIT an item
Since you are using bootstrap modals, when you edit an item, that modal will popup showing that info. What you need to do is send that item's country ID to that modal's form so that it can be preselected, so do the following change:
first in #editar_modal
<div class="form-group">
<label class="control-label">País:</label>
<select id="pais_input" name="pais">
<?php foreach ($array_pais as $pais) { ?>
<option value="<?php echo $pais ?>"><?php echo $pais ?></option>
<?php } ?>
</div>
then in '#edit_link', 'click', function (e) add:
$modal.find("#pais_input").val(info.pais);

How to add email field to Stripe checkout for one-time payment

I am selling a product on my website. I have Stripe setup and have been accepting orders. I added a email field, however it is not saving to Stripe.
I'm not sure what I am doing wrong, if someone could offer some help. The email field is towards the bottom of the code.
popup_html.php
<div class="modal fade" id="model_stripe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Credit Card Information</h4>
</div>
<div class="modal-body">
<div id="submit_card_info"></div>
<form class="form-horizontal te-ajax-form" id="upgrade_form" action="upgrade/payment" role="form">
<div class="form-group">
<label class="col-sm-3 control-label" for="">Card Number</label>
<div class="col-sm-9">
<input class="form-control required" name="cardnumber" id="cardnumber" type="text" placeholder="Card Number" value=""/>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="">CVC</label>
<div class="col-sm-9">
<input class="form-control required" name="cvc" id="cvc" type="text" placeholder="CVC" value="" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="">Expiration</label>
<div class="col-sm-9">
<div class="row">
<div class="col-xs-6">
<select id="expMonth" style="width:100%;padding:5px;">
<?php
$year = date('Y');
for ($i=1; $i < 13; $i++) {
?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
}
?>
</select>
</div>
<div class="col-xs-6">
<select id="expYear" name="year" style="width:100%;padding:5px;">
<?php
$year = date('Y');
for ($i=1; $i < 21; $i++) {
?>
<option value="<?php echo $year+$i ?>"><?php echo $year+$i ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="">E-mail Address</label>
<div class="col-sm-9">
<input class="form-control required" name="email" id="email" type="text" placeholder="Your E-mail Address" value=""/>
</div>
</div>
</form>
<form id="upgrade_form_token" class="te-ajax-form" action="inc/stripe_charge.php">
Stripe_handler.php:
<?php
require_once ('stripe/lib/Stripe.php');
define('PRICE',9.99);
$stripe = array(
'secret_key' => ' sk_test_665545656565656 ',
'publishable_key' => 'pk_test_35845754785784578'
);
Stripe::setApiKey($stripe['secret_key']);
?>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("<?php echo $stripe['publishable_key']; ?>");
jQuery(document).ready(function() {
jQuery('#model_stripe .extra_items input').change(function(){
if( jQuery(this).attr('name')=='info[addon3]' ){
if( jQuery(this).is(':checked') ) {
jQuery('#saNotes').show();
} else {
jQuery('#saNotes').hide();
}
}
var extra_for_stripe = 0;
jQuery('#model_stripe .extra_items input:checked').each(function(){
extra_for_stripe += Number(jQuery(this).val());
});
var notes = false;
var total_charges = extra_for_stripe+(<?php echo PRICE ?>);
jQuery('#upgrade_form_token').find('[name="amount"]').val(total_charges);
jQuery('#model_stripe .total_charges').text('$'+total_charges)
});
jQuery("#proceed_stripe").click(function(event) {
jQuery('#submit_card_info .payment-message').remove();
var card_no = jQuery('#cardnumber').val();
var cvc = jQuery('#cvc').val();
if(card_no.length==0){
var errorHTML = '<div class="payment-message alert alert-danger alert-dismissable fade in"><button class="close" data-dismiss="alert">×</button ><p>Card Number is required.</p></div>';
jQuery("#submit_card_info").prepend(errorHTML);
return false;
}
if(cvc.length==0){
var errorHTML = '<div class="payment-message alert alert-danger alert-dismissable fade in"><button class="close" data-dismiss="alert">×</button ><p>CVS is required.</p></div>';
jQuery("#submit_card_info").prepend(errorHTML);
return false;
}
// jQuery('#model_stripe .modal-footer button ').attr('disabled',true);
Stripe.createToken({
number: card_no,
cvc: cvc,
exp_month: jQuery('#expMonth').val(),
exp_year: jQuery('#expYear').val(),
}, stripeResponseHandler);
return false; // submit from callback
});
});
function stripeResponseHandler(status, response) {
if (response.error) {
jQuery('#upgrade_button').removeAttr("disabled");
// show the errors on the form
var errorHTML = '<div class="payment-message alert alert-danger alert-dismissable fade in"><button class="close" data-dismiss="alert">×</button ><p>'+response.error.message+'</p></div>';
jQuery("#submit_card_info").prepend(errorHTML);
}
else {
var form = jQuery("#proceed_stripe");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
jQuery('#upgrade_form_token input[name="stripeToken"]').val(token);
jQuery('#footer-form input[type="text"], #footer-form select').each(function(){
var h = '<input value="'+jQuery(this).val()+'" name="info['+jQuery(this).attr('name')+']" type="hidden">';
jQuery('#upgrade_form_token').append(h);
});
jQuery('#upgrade_form_token').submit();
}
}
function upgrade_form_token_success(form,resp){
jQuery('#submit_card_info .payment-message').remove();
if( resp.type=='success'){
var h = '<div style="display: block;font-size: 16px;padding-bottom: 10px;font-weight: bold;">Thanks for buying. Buy More</div>';
jQuery("#model_stripe .modal-body").html(h);
jQuery("#model_stripe .modal-footer").remove();
jQuery("#model_stripe .modal-backdrop").css({
'opacity':1,
'background-color':'#fff',
});
} else {
jQuery("#submit_card_info").append(bs_alert('danger',resp.msg ) );
}
}
</script>
Stripe has some good documentation on converting an existing form to a Stripe form # https://stripe.com/docs/stripe.js/switching.
(1) Note that you remove the the name attribute from all sensitive input fields ( as noted by michael above) Unless you are running a PCI compliant server, this is critical.
(2) When you parse your form and try and finally try and charge the card, you want to pass in the email address as receipt_email:customer#email.com (see https://stripe.com/docs/api#create_charge)
Hope this helps!

Categories