0

I am looking to process data in popup as ajax and display successful message inside popup only, Nothing is happening when I click on 'Submit An Offer' button, in console logs I saw uncaught error.

Popup Modal:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal<?php echo esc_html($post->ID);?>" data-whatever="@getbootstrap">Submit Offer</button>
                                    <div class="modal fade" id="exampleModal<?php echo esc_html($post->ID);?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Submit Your Offer for <?php echo esc_html($post->post_title);?></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
          <p class="statusMsg"></p>
        <form role="form">
     <div class="form-group row">
            <label class="col-md-12"  for="Name"><?php  esc_html_e( 'Name', 'ivproperty' ); ?><span class="red-star">*</span></label>
            <input  class="col-md-12" id="name" name="name" type="text" required>
         <imput type="hidden" id="id" value="<?php $post->ID ?>">
     </div>
        <div class="form-group row">
                <label for="email" class="col-md-12"><?php  esc_html_e( 'Email', 'ivproperty' ); ?><span class="red-star">*</span></label>
             <input class="col-md-12"  id="email" type="text" required>
     </div>
                <div class="form-group row">
                <label for="price" class="col-md-12"><?php  esc_html_e( 'Price', 'ivproperty' ); ?><span class="red-star">*</span></label>
             <input class="col-md-12"  id="price" type="number" required>
     </div>
        <div class="form-group row">
                    <label for="purchase_type" class="col-md-12"><?php  esc_html_e( 'Purchase Type', 'ivproperty' ); ?><span class="red-star">*</span></label>
<select class="col-md-12" id="purchase_type" required>
  <option disabled selected value> -- select an option -- </option>
  <option value="Cash">Cash</option>
  <option value="Conventional Loan">Conventional Loan</option>
  <option value="FHA Loan">FHA Loan</option>
  <option value="MSHDA Conventional Loan">MSHDA Conventional Loan</option>
  <option value="MSHDA FHA Loan">MSHDA FHA Loan</option>
  <option value="Land Contract">Land Contract</option>
</select>
     </div>
 <div class="modal-footer">                 
                                        
                            <button type="button" class="btn btn-primary submitBtn" onclick="submitContactForm()">Submit An Offer</button>
                        </div>  
</form>
      </div>
      <div class="modal-footer">
        <?php echo esc_html($post->ID);?>
      </div>
    </div>
  </div>
</div>

Script:

<script>
function submitContactForm(){
    var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    var name = $('#name').val();
    var email = $('#email').val();
    var price = $('#price').val();
    var purchase_type = $('#purchase_type').val();
    var id = $('#id').val();
    if(name.trim() == '' ){
        alert('Please enter your name.');
        $('#inputName').focus();
        return false;
    }else if(email.trim() == '' ){
        alert('Please enter your email.');
        $('#inputEmail').focus();
        return false;
    }else if(email.trim() != '' && !reg.test(email)){
        alert('Please enter valid email.');
        $('#inputEmail').focus();
        return false;
    }else{
        $.ajax({
            type:'POST',
            url:'<?php get_template_directory_uri(); ?>/submitoffer.php',
            data:'OfferSubmit=1&name='+name+'&email='+email+'&price='+price+'&purchase_type='+purchase_type+'&id='+id,
            beforeSend: function () {
                $('.submitBtn').attr("disabled","disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success:function(msg){
                if(msg == 'ok'){
                    $('#name').val('');
                    $('#email').val('');
                    $('#price').val('');
                    $('#purchase_type').val('');
                    $('.statusMsg').html('<span style="color:green;">Offer Submitted Successfully.</p>');
                }else{
                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                $('.submitBtn').removeAttr("disabled");
                $('.modal-body').css('opacity', '');
            }
        });
    }
}
</script>

Error I'm seeing in console log: enter image description here

What I want is when user open's popup and fill the form and then submit it, data should get processed inside popup only and display the status result.

This question doesn't answer my problem as pop-up modal stopped working after applying solution provided in a little similar question.

qazidev
  • 17
  • 5
  • 2
    Did you import jquery? I don't see it in your HTML – Liftoff Oct 28 '21 at 05:13
  • thanks for reply, Yes I am using wordpress and JQuery is imported, popup is working fine but this form submission is not doing anything. – qazidev Oct 28 '21 at 05:15
  • 2
    Are you sure? `$ is not a function` usually means jQuery isn't loaded. If you're sure, are you using any other conflicting libraries (MooTools for example)? You can wrap your code inside a `jQuery.noConflict` to prevent conflicts if so. – Liftoff Oct 28 '21 at 05:17
  • I deregistered default wordpress JQuery and added `https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js` in header. but pop-up stopped working, popup is not coming up, getting error when clicking on pop-up trigger button: Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': – qazidev Oct 28 '21 at 05:35
  • [Pop Up Error Screenshot](https://paste.pics/0e629052b083aedf373891a99be1208d) – qazidev Oct 28 '21 at 05:44
  • @qazidev You are using jquery v1.12.4. The 3.1.1 is never loaded – Carsten Løvbo Andersen Oct 28 '21 at 06:18
  • I did it using custom code in functions.php also checked source code. – qazidev Oct 28 '21 at 06:45
  • [Please do not post images of code/error messages.](https://meta.stackoverflow.com/a/285557). Copy-paste the relevant parts of your error msg and add them to your question. – Don't Panic Oct 28 '21 at 08:02
  • Did you try searching for your error msg? There are many duplicates here on SO, with many solutions, have you tried all of those? – Don't Panic Oct 28 '21 at 08:05
  • Does this answer your question? [$ is not a function - jQuery error](https://stackoverflow.com/questions/3931529/is-not-a-function-jquery-error) – Don't Panic Oct 28 '21 at 08:05
  • @Don'tPanic no it doesn't answer my question that's why I created question, please try to help – qazidev Oct 28 '21 at 08:22
  • So you have tried everything suggested in that question? Eg using `jQuery` instead of `$`, and putting your code inside `jQuery(document).ready(function ($) {`? – Don't Panic Oct 28 '21 at 08:27
  • Yes it doesn't work – qazidev Oct 28 '21 at 08:39

1 Answers1

0

Try to put your function inside this:

$(function() {
 // your code here
});

Or this:

(function( $ ) {
 // your code here
} )( jQuery );
  • Thanks, can you just modify my code and edit in your answer, I am getting confused. – qazidev Oct 28 '21 at 06:14
  • I don't see how this answer should fix the OP's problem. The indication is clearly that jQuery has not been loaded correctly. – Carsten Løvbo Andersen Oct 28 '21 at 06:15
  • @CarstenLøvboAndersen jquery could be loaded internally in a different js, it is not said that if it is not seen in the console it is not loaded. – Michela Durazzi Oct 28 '21 at 06:18
  • @qazidev example: (function( $ ) { function submitContactForm(){...} } )( jQuery ); Same for other test. – Michela Durazzi Oct 28 '21 at 06:19
  • @Michela-Durazzi I tried but still facing same issue. – qazidev Oct 28 '21 at 06:44
  • @qazidev I'm sorry, then you should show us your site, so we don't try to – Michela Durazzi Oct 28 '21 at 06:59
  • definitely you can go to my site and click on Submit offer button to check or check source code: [check page click here](https://submitalloffers.stagingweb.net/properties/) – qazidev Oct 28 '21 at 07:02
  • jQuery is loaded. First error I see is this block: All of this code must be inserted in one of my example above. Like this: (function( $ ) { $('#exampleModal4179').on('show.bs.modal', function (event) {...}) } )( jQuery ); Please change it, then I refresh and see if it resolves first error. – Michela Durazzi Oct 28 '21 at 07:20
  • thanks, I added modal code inside `$(function() { });` as you instructed, can you please refresh and check. – qazidev Oct 28 '21 at 07:29
  • no, the code I write in last comment (function( $ ) ... it's the second of my first example – Michela Durazzi Oct 28 '21 at 07:37
  • When I put modal script in `(function( $ ) { } )( jQuery );` popup stopped working, can you please check now. – qazidev Oct 28 '21 at 08:17
  • Now the code is executed, so this is the right solution. Let's move on to the next problem. Please comment these two lines, I think the error is here: // modal.find('.modal-title').text('New message to ' + recipient) // modal.find('.modal-body input').val(recipient) The comment is only used for identify the lines with errors. – Michela Durazzi Oct 28 '21 at 08:46