I'm having a frustrating problem with a form submit. My functions are all called (checked through log file) but request-->post('..') doesn't return anything. I would be eternally thankfull to anyone who helps solve this headache.
php file loads the template, fills the form with data. form contains a button which on click calls javascript function addAllToCartProject(). this function calls the addAll function in the initial php file where I try to get the data from the form with no success.
The code I'm working on is for an OpenCart module.
template file---> project_edit.tpl
<div id="tab-products" class="tab-content">
<form method="post" enctype="multipart/form-data" id="form2">
<div class="buttons"><div class="left">
<input type="button" value="<?php echo $button_add_products; ?>" id="pselect" class="button" />
<input type="button" value="<?php echo $button_add_all_cart; ?>" id="addallltocart" onclick="addAllToCartProject();" class="button" />
<input type="button" value="<?php echo $button_print; ?>" id="printproject" class="button" />
</div></div>
<div class="cart-info">
<table>
.
.
.
</table>
<input type="hidden" name="akey" value="<?php echo $akey; ?>" />
<input type="hidden" name="eid" value="<?php echo $eid; ?>" />
</form>
</div>
javascript function
function addAllToCartProject() {
$.ajax({
url: 'index.php?route=account/projects/addAll',
type: 'post',
data: $('#form2').serialize(),
dataType: 'json',
success: function(json) {
$('.success, .warning, .attention, .information, .error').remove();
if (json['error']) {
$('#notification').html('<div class="warning" style="display: none;">' + json['error'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.warning').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
} else {
$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
}
php file-->projects.php
.
.
.
if (isset($this->request->get['project_id'])) {
$printlink = $this->url->link('projects/projects_print', 'project_id=' . $eid . '&akey=' . $akey);
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/projects_edit.tpl')) {
$this->template = $this->config->get('config_template') . '/template/account/projects_edit.tpl';
} else {
$this->template = 'default/template/account/projects_edit.tpl';
}
} else { ..............}
.
.
.
public function addAll() {
$this->language->load('checkout/cart');
$this->language->load('account/projects');
$this->load->model('account/projects');
if (isset($this->request->post['eid'])) {
$eid = $this->request->post['eid'];
} else {
$eid = '0';
$file="log.txt";
$log=fopen($file,'a');
fwrite($log,"\n eid is not set ");
fclose($log);
}
if (isset($this->request->post['akey'])) {
$akey = $this->request->post['akey'];
} else {
$akey = '0';
$file="log.txt";
$log=fopen($file,'a');
fwrite($log,"\n akey is not set ");
fclose($log);
}
.
.
.
}
May be try to give names attribute to your form elements:
<div id="tab-products" class="tab-content">
<form method="post" enctype="multipart/form-data" id="form2">
<div class="buttons"><div class="left">
<input name = "products" type="button" value="<?php echo $button_add_products; ?>" id="pselect" class="button" />
<input name = "cart" type="button" value="<?php echo $button_add_all_cart; ?>" id="addallltocart" onclick="addAllToCartProject();" class="button" />
<input name = "print" type="button" value="<?php echo $button_print; ?>" id="printproject" class="button" />
</div></div>
<div class="cart-info">
<table>
.
.
.
</table>
<input type="hidden" name="akey" value="<?php echo $akey; ?>" />
<input type="hidden" name="eid" value="<?php echo $eid; ?>" />
</form>
</div>
Related
In my code I send $id to Java Script section and it returns the same value inside <p id="retid"></p> and I can't get the value as integer inside $text variable.
`while ($row = $result->fetch_assoc())
{
$id = $row["id"];
$regname = $row["reg-name"];
echo '<button class="btnicon btndel" type="button" name="answer" onclick="showDiv('.$id.')"><i class="fa fa-trash"></i> '.$regname.'</button>';
}
} else {
//header("Location: login.php?Msg=wrongPass"); }
}
?>
</div>
<div id="DelMessageDiv" style="display:none;" class="answer_list" >
<form action="_modify.php" method="get">
<div id="alerts">
<?php
$text = '<p id="retid"></p>';
echo $text.'-+-'.strip_tags($text,'<p>').'
Are you sure for delete?
<input name="id" type="hidden" value="'.strip_tags($text).'" />
<button class="btnicon btndel" type="submit" name="operation" value="0+regDelete">Yes Delete</button>
';
?>
</div>
</form>
</div>
</div>
<?php include '_footer.php'; ?>
<script src="js/extention/choices.js"></script>
<script>
const choices = new Choices('[data-trigger]',
{
searchEnabled: false
});
function showDiv(x) {
document.getElementById('DelMessageDiv').style.display = "block";
document.getElementById('retid').innerHTML = x;
}
</script>`
Thanks for all helps.
I create a form with hidden input forms that submit the value to a PHP script and store each value in an array of sessions by reloading the page with AJAX. It returns an HTML success alert message to <p id ="msg"></p>. I need help on how to send $count to <p id="count"></p> and success alert message to <p id ="msg"></p> at the point of success: in AJAX. And also I will like the success alert to disappear after 3 seconds of the display. Below is my code:
my_add_cart.php
<?php
session_start();
$_SESSION['title'][]=$_POST['title'];
$_SESSION['price'][]=$_POST['price'];
$_SESSION['img_src'][]=$_POST['img_src'];
$count = count($_SESSION["title"]);
echo $count;
echo '<div class="alert">
<span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>
<center>Product added successfully to cart.</center>
</div>';
exit();
?>
Above is my_add_cart.php and below is my HTML and javascript:
<script type="text/javascript">
function clickButton(){
var title=document.getElementById('title').value;
var price=document.getElementById('price').value;
var img_src=document.getElementById('img_src').value;
$.ajax({
type:"post",
url:"my_add_cart.php",
data:
{
'title' :title,
'price' :price,
'img_src' :img_src
},
cache:false,
success: function (html)
{
$('#msg').html(html);
}
});
return false;
}
</script>
<html>
<p id="msg"></p>
<p id="count"></p>
<form onsubmit="clickButton()">
<input type="hidden" value="<? echo $title ?>" name = "title" id="title" >
<input type="hidden" value="<? echo number_format($price); ?>" name = "price" id="price" >
<input type="hidden" value="<? echo "https://mikeandcathy.com.ng/admin/UploadFolder/".$row_product_img[0]; ?>" name = "img_src" id="img_src">
<button type="submit" id="add_to_cart" name="add_to_cart" class="btn btn-outline-secondary btn-sm" value="Add to cart" onclick="return clickButton();">Add Cart</button>
</form>
</html>
I Suggest turning your server code into a json api
Solution
change my_add_cart.php to this
<?php
session_start();
$_SESSION['title'][]=$_POST['title'];
$_SESSION['price'][]=$_POST['price'];
$_SESSION['img_src'][]=$_POST['img_src'];
$count = count($_SESSION["title"]);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(
[
'count' => $count,
'message' => '<div class="alert"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span> <center>Product added successfully to cart.</center></div>';
]
);
exit();
?>
change your frontend code to this
<script type="text/javascript">
function clickButton(){
var title=document.getElementById('title').value;
var price=document.getElementById('price').value;
var img_src=document.getElementById('img_src').value;
$.ajax({
type:"post",
url:"my_add_cart.php",
data:
{
'title' :title,
'price' :price,
'img_src' :img_src
},
cache:false,
success: function (data)
{
$('#msg').html(data['message']);
$('#count').html(data['count']);
}
});
return false;
}
</script>
<html>
<p id="msg"></p>
<p id="count"></p>
<form onsubmit="clickButton()">
<input type="hidden" value="<? echo $title ?>" name = "title" id="title" >
<input type="hidden" value="<? echo number_format($price); ?>" name = "price" id="price" >
<input type="hidden" value="<? echo "https://mikeandcathy.com.ng/admin/UploadFolder/".$row_product_img[0]; ?>" name = "img_src" id="img_src">
<button type="submit" id="add_to_cart" name="add_to_cart" class="btn btn-outline-secondary btn-sm" value="Add to cart" onclick="return clickButton();">Add Cart</button>
</form>
</html>
I have a database which consists of question_id, question, and options. Where I'll display the question and options. When the user clicked on submit I want to store the option they clicked and the question_id.
I am able to store the option they clicked but want to know how to store the question_id.
$user_email = $_SESSION['email'];
$query1="SELECT * FROM votes WHERE user_email='$user_email'";
$query2="SELECT * FROM poll_question WHERE status='1'";
$fetch2 = mysqli_query($con,$query2);
<html>
<head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="row">
<div class="col-md-6">
<?
while( $row2 = mysqli_fetch_array($fetch2))
{
?>
<form method="post" class="poll_form">
<h3><?echo $row2['question']?>?</h3>
<br />
<div class="radio">
<label><h4><input type="radio" name="poll_option" class="poll_option" value=<?echo $row2['option1']?> /><?echo $row2['option1']?></h4></label>
</div>
<div class="radio">
<label><h4><input type="radio" name="poll_option" class="poll_option" value=<?echo $row2['option1']?> /> <?echo $row2['option2']?></h4></label>
</div>
<br />
<?php
if( $count >0)
{ ?>
<button disabled="disabled" alt="<?php echo $row2['question_id']; ?>" rel="<?php echo $user_email; ?>" class="btn btn-primary poll_option" title="<?php echo $ip; ?>">Submit</button>
<h>You selected : <? echo $row1['vote_option']; ?></h>
<br>
<p id="demo"></p>
<?php
}
else
{ ?>
<button alt="<?php echo $row2['question_id']; ?>" rel="<?php echo $user_email; ?>" class="btn btn-primary poll_option" title="<?php echo $ip; ?>">Submit</button>
<?
}
?>
</form>
<? }
} ?>
<br />
</div>
</div>
<br />
<br />
<br />
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
$(".poll_form").submit(function(event){
event.preventDefault(); //prevents the form from submitting normally
var poll_option = '';
$('button.poll_option').each(function(){
if($(this).prop("checked"))
{
poll_option = $(this).val();
var option=poll_option;
}
var url = '<?php echo SITE_URL; ?>';
});
$.post("poll_vote.php",$('.poll_form').serialize(),
function(data)
{
if(data=="User Created Success"){
window.setTimeout(function () {
location.href ="<?php echo SITE_URL; ?>poll.php";
}, 100);
}
else{
$("#result").html(data);
}
}
);
//to reset form data
});
});
<? } ?>
</script>
</html>
Using th below function I am sending the poll_option to other where I kept the inset operation. Now I want to send the question_id also
Create a hidden input filed in your form
<input type="hidden" name="question_id" value="<?php echo $row2['question_id']; ?>">
Create a hidden input field in your form.
<input type="hidden" id="question_id" name="question_id" value="<?= $row2['question_id'] ?>">
PHP solution:
In this case your question_id will be included in the POST.
You can then access it by calling $_POST['question_id'].
Jquery solution:
In Jquery you can also access it by:
$("#question_id").val();
-when i update with ajax , he changed but he only took to 'id' not the new variable
But I want to insert my E-Mail to database through Ajax. I don't want my page to get redirected, because every time the page got refreshed, null value got inserted to the database..
1)and ajax
function editdata(str){
var id= str;
var name = $('#nameofequipe-'+str).val();
$.ajax({
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"nameofequipe="+name+"&id="+id,
success:function(data){
alert('success update data ');}
2)and modal bootstrap
<div
class="modal fade"
id="edit-<?php echo $row['id']; ?>"
role="dialog"
aria-labelledby="updatelLabel-<?php echo $row['id']; ?>
<input
type="hidden"
id="<?php echo $row['id']; ?>"
value="<?php echo $row['id']; ?>" >
<input
type="text"
class="form-control"
id="nameofequipe-<?php echo $row['id']; ?>"
value="<?php echo $row['equipe']; ?>">
if somoene know how i can do This can He help me
//use this in script section
function editdata(str){
var id= str;
var name = $('#nameofequipe-'+str).val();
$.ajax({
dataType: "json",
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"{nameofequipe:"+name+",id:"+id+"}",
success:function(data){
alert('success update data ');
}
//whereas this one in your PHP file where you use this data nameOfEquipe and Id to update you database.
if(isset($_POST['nameofequipe'], $_POST['id']) {
$nameOfEquip=$_POST['nameofequipe'];
$id=$_POST['id'];
}
This Is all code
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
</button>
</div>
<form>
<div class="modal-body">
<input type="hidden" id="<?php echo $row['id']; ?>" value="<?php echo $row['id']; ?>" >
<input type="text" class="form-control" name="nameofequipe-<?php echo $row['id']; ?>" value="<?php echo $row['equipe']; ?>">
</div>
<button type="submit" onclick='editdata(<?php echo $row['id']; ?>)' class="btn btn-primary">Save changes</button>
</div>
</form>
ajax and js
function editdata(str)
{
var id= str;
var name= $('#nameofequipe-'+str).val();
$.ajax ({
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"nameofequipe="+name+"&id="+id,
success: function(data){
alert("Data Save: " + data);
}
}); }
update. PHP
$id = $_POST['id'];
$name=$_POST['nameofequipe'];
$result = $o->SelectQuery("UPDATE rh_equipes SET equipe='$name' WHERE id='$id'");
if($result){
echo 'data update';
}
i want to insert picture in particular name but there is no insert and some facing error
my table like this
how can insert pic in front of particular name
config.php file is
<?php
session_start();
$db = new mysqli('localhost','root','','dharmesh');
?>
and code is here :
<?php
include_once('config.php');
if(isset($_POST['family_member_btn'])) {
$family_member = $_POST['family_member'];
}
if(isset($_POST["submit4"])) {
for($i=0;$i<$_POST['num'];$i++) {
$insert = $db->query("INSERT into `multiple_insert` (`f_name`,`m_name`,`l_name`,`birth_date`) values ('".$_POST['f_name'][$i]."','".$_POST['m_name'][$i]."','".$_POST['l_name'][$i]."','".$_POST['b_date'][$i]."')");
if($insert) {
echo "<script>alert('insert successfully');</script>";
} else {
echo "<script>alert('!!!insert unsuccessfully');</script>";
}
}
}
if(isset($_POST["upload"])) {
for($i=0; $i<$_POST['img'];$i++) {
$imag = $_FILES['f_name_pic']['name'][$i];
$tmp = $_FILES['f_name_pic']['tmp_name'][$i];
$dir = "images/".$imag;
move_uploaded_file($tmp,$dir);
$query = $db->query("UPDATE `multiple_insert` SET `picture`='$dir' WHERE f_name='".$_POST['f_name'][$i]."' where user_id='1'");
}
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="">
<?php
$select = $db->query("SELECT * from multiple_insert where user_id='1'");
while($select_f_name = $select->fetch_assoc()){
echo "<input type='hidden' value='1' name='img' />";
echo "<p style='background-color:red;color:yellow;width:5%;'>".$select_f_name['f_name']."</p>";
echo "<span><input type='file' name='f_name_pic[]' /></span><br><br>";
}
echo "<button name='upload'>Uplaod</button><br><br>";
?>
<label><font size="2">HOW MANY MEMBER IN YOUR FAMILY ?</font></label><br><br>
<input type="text" name="family_member" class="form-control" />
<button type="submit" name="family_member_btn" class="btn btn-lg btn-info" /><span>SUBMIT</span></button><br><br>
<?php
for($i=1;$i<=$family_member;$i++) {
?>
<input type="hidden" value="<?php echo $family_member;?>" name="num" />
<label><b><?php echo "RECORED # ".$i;?></b></label><br>
<label><font size="2">First Name</font></label>
<input type="text" name="f_name[]" class="form-control" /><br>
<label><font size="2">Middle Name</font></label>
<input type="text" name="m_name[]" class="form-control" ><br>
<label><font size="2">Last name</font></label>
<input type="text" name="l_name[]" class="form-control" /><br>
<label><font size="2">birthdate</font></label>
<input id="datepicker3" class="form-control" name="b_date[]" type="text" />
<hr style="border:solid 2px rgba(0,0,0,0.2);">
<?php } ?>
<button class="btn btn-info btn-cons from-left pull-right" type="submit" name="submit4">
<span>SUBMIT</span>
</button>
</form>
</body>
</html>
how can i done this please send me updated code for this...
You cannot do that! The interface you are looking at, is phpMyAdmin---unless you are willing to hack the core code (HIGHLY NOT RECOMMENDED TO MODIFY CORE CODE OF ANY PROJECT!).