简体   繁体   中英

How to pass a value in jquery function without using textbox

https://www.codingsnow.com/2021/01/create-php-send-email-contact-form.html In this code, how do i send subject as "hi", what do I need to change in the function, I tried setting it as a string but it didn't work, also tried sending it directly in the data:{}..it didnt work..

 function sendEmail() { var name = $("#name"); var email = $("#email"); var subject = $("#subject"); var body = $("#body"); if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) { $.ajax({ url: 'sendEmail.php', method: 'POST', dataType: 'json', data: { name: name.val(), email: email.val(), subject: subject.val(), body: body.val() }, success: function(response) { $('#myForm')[0].reset(); $('.sent-notification').text("Message Sent Successfully."); } }); } } function isNotEmpty(caller) { if (caller.val() == "") { caller.css('border', '1px solid red'); return false; } else caller.css('border', ''); return true; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h4 class="sent-notification"></h4> <form id="myForm"> <h2>Send an Email</h2> <label>Name</label> <input id="name" type="text" placeholder="Enter Name"> <br><br> <label>Email</label> <input id="email" type="text" placeholder="Enter Email"> <br><br> <label>Subject</label> <input id="subject" type="text" placeholder=" Enter Subject"> <br><br> <-----------------------------------------------------will disable this..and then need to send subject as "hi" instead, from function <p>Message</p> <textarea id="body" rows="5" placeholder="Type Message"></textarea> <br><br> <button type="button" onclick="sendEmail()" value="Send An Email">Submit</button> </form>

If you just want to send "hi" for the subject instead of the value the user typed in the <input id="subject" you just can hardcode it directly in the ajax method where you define the data:

$.ajax({
  ...
  data: {
    name: name.val(),
    email: email.val(),
    subject: 'hi',
    body: body.val()
  },
  ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM