简体   繁体   中英

How to prevent a form from opening action url and only print when submitted?

i am building a form and when i submit it it opens the action url. I want to submit the form on click button which should not open the target url but submit the request. as well as i want to print a message after submit and form should be cleared after submit.

<form id="contact" action="https://control.msg91.com/api/sendhttp.php" method="post">
<h3>Send SMS</h3>
<h4>Build in Process</h4>
<fieldset>
  <input name="authkey" type="hidden" value="auth key as required"/>
  <input name="mobiles" placeholder="Mobile Number" type="text" tabindex="1" maxlength="10" required >
</fieldset>
<fieldset>
<textarea name="message" placeholder="Type your message here...." tabindex="2" maxlength="320" required></textarea>
</fieldset>
<input name="sender" type="hidden" value="FAKEin" />
<input name="route" type="hidden" value="4" />
<input name="country" type="hidden" value="0" />
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
</form>

any help how can i do this? also can i add a hidden text in message tab that should add to the message tab in post/get as + instead of & eg. actionurl.php?authkey=custommade&mobiles=9999999999&message=orginal+message+hidden+message&sender=FAKEin&route=4&country=0&submit=

You can also check the source code of page https://www.tricksbygoogle.com/sms/sms.php

Based on your question, and the comments it looks like you're going to have to do a little bit of research. Here are some tips though.

  1. If you would like to include the functions from a page without actually visiting the page, than you can use what is called an include statement. This will keep the browser from visiting that page while still executing it. - http://php.net/manual/en/function.include.php
  2. To display a message you're going to need to hide and show the element with javascript. I would suggest viewing this question - Hide div after a few seconds

Basically on submit, you're going to want to check for the variables from your form. You would run a php if statement.

Then, if those variables exist you are going to want to include your action page.

In the same if statement, you're going to want to echo a <div> with a class that has some javascript attached to it to hide it after a few seconds.

The form will automatically clear on submit.

if($variable != null){
    include '/action.php' // --- you can add some GET variables to the end of this if you would like to.
    echo '<div id="message">Message sent</div>'
}

Basically You can't . The only solution here I see , is using ajax query and use javascript to clear form. This example I provide is no redirections at all. What means you page will not be reloaded.

Maybe little jquery will help.

var result_func = function(response){
  if(response.allOk){
    $this.reset(); 
 }
}

$('#contact').submit(function(e){
  e.preventDefault()l
  var $this = $(this);
  var data = $this.serialize();
  $.post($this.attr('action'),data,result_func.bind($this));
});

Header location will work , but user still will be redirected.

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