-2

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

enter image description here

enter image description here

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;
}

?>

RINKS
  • 71
  • 7
  • I don't why this is happening – RINKS Jan 23 '20 at 13:11
  • so why this is happening if i am echoing the variable then why i am not able to send it – RINKS Jan 23 '20 at 13:13
  • Impossible for us to tell, with the rather chaotic and incomplete info you have given so far. Have you first of all checked the HTML output your PHP part creates, to verify that the form is correct? – 04FS Jan 23 '20 at 13:15
  • yes this sending of hidden type is working for other files, want to see complete file – RINKS Jan 23 '20 at 13:16
  • Just use ajax serialize method and get response from php, to see the result if values passing to php, echo out php and see the result, simple and easy. Here is example ajax : https://stackoverflow.com/a/59874137/12232340 and change php variables in inputs like this `` Last point use POST method instead of GET –  Jan 23 '20 at 13:26
  • @mickmackusa please remove the duplicate tag this error need to removed, you have power to tag but that does not mean to press down the low power people – RINKS Jan 24 '20 at 04:06
  • @Dilek, I have updated my post please help me out – RINKS Jan 24 '20 at 04:13
  • @04FS, please remove duplicate tag there is no question similar to my problem in SO – RINKS Jan 24 '20 at 04:32
  • @Dilek iu am using `GET` because i have value in my URL as `http://localhost/CRM/lead/index.html?lead_id=17` – RINKS Jan 24 '20 at 04:35

1 Answers1

2

You can use this:

<input type="hidden" id="foos" name="any" value="bar" />
<script>
    $('input[name=any]').val();
</script>
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Jayshri Ghoniya
  • 266
  • 1
  • 9
  • 3
    `You can Use like that` is not an explanation. – mickmackusa Jan 23 '20 at 13:15
  • but i want to use PHP variable – RINKS Jan 23 '20 at 13:15
  • @mickmackusa please remove the duplicate tag this error need to removed, you have power to tag but that does not mean to press down the low power people – RINKS Jan 24 '20 at 04:05
  • @Jayshri Ghoniya please see in this – RINKS Jan 24 '20 at 04:45
  • 1
    I have not closed your question as a punishment, the duplicate link is provided to give you solutions and educational guidance. This answer is low value and does not demonstrate how to generate a javascript variable. The "solution" in your question does not resemble any of the mark up that you have provided. This question is Unclear and will be very unlikely to help future researchers -- especially compared to the other page that is referenced. – mickmackusa Jan 24 '20 at 07:11
  • 1
    This is a code-only answer that does not show a complete solution nor appreciates your provide html elements -- I am not sure why anyone would UV it or acept it. – mickmackusa Jan 24 '20 at 07:21
  • @mickmackusa, don't worry just thanks – RINKS Jan 24 '20 at 07:51
  • While this code may resolve the OP's issue, it is best to include an explanation as to how/why your code addresses it. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman Jun 05 '20 at 00:19