Solution:
This below not worked
// var user_id = $('#attach-old-campaign-form').find('[name="userid"]').val();
// var added_on = $('#attach-old-campaign-form').find('[name="addedon"]').val();
This below worked
var lead_id = $('#attach-old-campaign-form-leadid').val();
var user_id = $('#attach-old-campaign-form-userid').val();
var added_on = $('#attach-old-campaign-form-addedon').val();
Question:
I am able to echo a PHP variable and also can see my variable assigned to input type hidden variables
but when i send the HTML form with hidden type NO variable is received at JS function and also it raise error in my PHP server file attach_old_campaign.php
.
PIC
So I have three files index.html
, attach_old_campaign.js
and attach_old_campaign.php
.
I have below part of code of index.html
:
<?php
$user_id = $_SESSION['User_Id'];
$lead_id = $_GET['lead_id'];
echo "User id: ".$user_id;
echo "Lead_Id: ".$lead_id;
if(function_exists('date_default_timezone_set'))
{
date_default_timezone_set("Asia/Kolkata");
}
$today = date("Y-m-d H:i:s");
?>
<input type="hidden" name="leadid" value="<?php echo $_GET['lead_id'] ?>" >
<input type="hidden" name="userid" value= "<?php echo $_SESSION['User_Id'] ?>" >
<input type="hidden" name="addedon" value="<?php echo date('Y-m-d H:i:s') ?>" >
I am getting error at above input type hidden.
Also i am not able to console the output at my JS code file attach_old_campaign.js
.
$(document).ready(function(){
var delay = 1000;
$('[name="attach_old_campaign_submit"]').click(function(e){
e.preventDefault();
var lead_id = $('#attach-old-campaign-form').find('[name="leadid"]').val();
var campaign_arr = [];
// Initializing array with Checkbox checked values
$("input[name='checkedcampaign_arr']:checked").each(function(){
campaign_arr.push(this.value);
});
var user_id = $('#attach-old-campaign-form').find('[name="userid"]').val();
var added_on = $('#attach-old-campaign-form').find('[name="addedon"]').val();
console.log(lead_id)
console.log(user_id)
console.log(added_on)
console.log(campaign_arr)
$.ajax({
type: "POST",
url: "./server/attach_old_campaign.php",
data: {
"lead_id": lead_id,
"campaign_arr": campaign_arr,
"user_id": user_id,
"added_on": added_on
}
});
});
});
My PHP server file attach_old_campaign.php
<?php
// send a JSON encoded array to client
include('../../server/connection.php');
/* check connection */
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$leadid = $_POST['lead_id'];
$campaign_arr = $_POST['campaign_arr'];
$userid = $_POST['user_id'];
$addedon = $_POST['added_on'];
foreach ($campaign_arr as $campaign_id) {
$stmt = $conn->prepare("INSERT INTO `tbl_mapping_lead_and_campaign` (`Map_Lead_Id`, `Map_Campaign_Id`, `MappingAddedBy`, `MappingAddedOn`) VALUES (?,?,?,?) ");
$stmt->bind_param( "ssss", $leadid, $campaign_id, $userid, $addedon );
if($stmt->execute()){
echo "<span style='color:green;'>
Campaign information mapped to Lead successfully</span>";
}else{
echo "<span style='color:red;'> Campaign information NOT mapped to Lead..
</span><p></p>";
printf("Error: %s\n", $conn->error);
}
}
}else{
echo "Error: %s".$conn->error;
}
?>