Disable submit button after submit and page refresh - javascript

What i want is pretty simple, i have a button that submits a value to my database once clicked, what i need is to diable the button parmanently after value has been submited, i have searched and the codes i have seen all enable the button when the page refreshes.. please can someone help me out?
My form and button code as follows:
<form action="<?php echo $editFormAction; ?>" method="POST" name="Status2" id="Status2" onSubmit="return confirm('Are you sure you want to submit?');">
<input name="Confirm2" type="checkbox" id="Confirm2" value="Confirmed" checked="CHECKED" style="display:none;">
<label for="Confirm2"></label>
<input name="UpdateButton3" type="submit" class="art-button" id="UpdateButton3" value="Confirm"/>
<input name="UserIDHiddenField4" type="hidden" id="UserIDHiddenField4" value="<?php echo $row_User['UserID']; ?>">
<input name="Purge2" type="checkbox" id="Purge2" value="You Will Be Rematched Soon!" checked="CHECKED" style="display:none;">
<label for="Purge2"></label>
<input type="hidden" name="MM_update" value="Status2">
</form>

First of all if you submit it, this has nothing to do with javascript. Save this in your database as #georoot says. Then when you submit, save a value in the database. If that value is set, you can disable the button by just HTML:
<input type="submit" <?php if($valueFromDatabase==1){ ?> disabled <?php } ?> >

Best solution could be to set a cookie on client system and/or get user IP and save it into your database, but you should keep in mind, non of them are permanent ! The IP can be changed and the cookie can be removed ! But you can use both cookie and IP check.
Then for each page load, just check if cookie exist (compare the value with ur database value) and/or client IP is already exist in database.
Or if you know who is on the page (if it's a logged user) just add a field in your database and set value = 1 or = 0 and check the field each time it's loaded for same field and for same user.
I wouldn't recommend to use IP check without cookies ! Because it can change frequency !

Related

AJAX POST not sending all data

I have the following HTML code within a form and the top one (policy) sends in post but the second one does not (mfa). I'm lost at why this is happening, any ideas?
<label class="switch switch-flat">
<input class="switch-input" type="checkbox" id="policy" method="post" name="policy" <?php if($row['policy'] === '1') echo 'checked="checked"';?> />
<span class="switch-label" data-on="Yes" data-off="No"></span>
<span class="switch-handle"></span>
</label>
<label for="policy">Does the vendor organization have a written Information Security policy?</label>
</td>
</tr>
<tr>
<td>
<div class="col-md-12">
<label class="switch switch-flat">
<input class="switch-input" type="checkbox" name="mfa" id="mfa" method="post" <?php if($row['mfa'] === '1') echo 'checked="checked"';?> />
<span class="switch-label" data-on="Yes" data-off="No"></span>
<span class="switch-handle"></span>
</label>
<label for="mfa">Do they support and utilize multi-factor authentication?</label>
</td>
Here's my ajax to send the post data. When I browse in the Dev tools for headers being sent I see a lot of the other HTML names going but most of the ones listed as toggle checkboxes aren't being sent except for the top one 'policy'.
<script type='text/javascript'>
/* attach a submit handler to the form */
$('#update').submit(function(e){
e.preventDefault();
$.ajax({
url:'updateVendor.php',
type:'post',
data:$('#update').serialize(),
success:function(){
//whatever you wanna do after the form is successfully submitted
}
});
});
</script>
A checkbox is only considered to have a value when it is checked. Otherwise, it is not included in the submitted form data.
From the MDN website:
Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all. If you wanted to submit a default value for the checkbox when it is unchecked, you could include an <input type="hidden"> inside the form with the same name and value, generated by JavaScript perhaps.
A trick to circumvent this limitation is to include a hidden input with a default value. E.g.
<input name="mfa" type="hidden" value="0">
<input name="mfa" type="checkbox" value="1">
With this approach, a value for mfa will always be submitted with the form data.

Retain value of radio button after validation php

My issue is once user click submit the form should check for all questions that the radio button is checked.
It should show the err message on the questions where the user didn't answer.
But right now the form refreshed everytime after the err message is shown and the
previous checked radio button is lost.I also have javascript timer going on,but
since the page get refreshed ,the timer starts all over again.
Im newbie to php,kindly guide me through this issue.Any help is appreciated.
<?php session_start()?>
<?php
$errorexist=false;
if (isset($_POST["submit"])){
for ($y=0;$y<5;$y++)
{
if(empty($_POST["question".$y])){
$errorexist=true;
${"err".$y}="* Please answer question ".($y+1)."<br>";
}
}
if (errorexist==false){
header ("Location: result.php");
exit;}
} ?>
html part of the code
<li>
<span class="error"><?php echo $err0?></span>
<h3>What is the smallest prime number?</h3>
<input type="radio" name="question0" value="A"<?php echo ($question0=='A')? 'checked':''; ?> />2<br>
<input type="radio" name="question0" value="B"<?php echo ($question0=='B')? 'checked':''; ?> />1<br>
<input type="radio" name="question0" value="C"<?php echo ($question0=='C')? 'checked':''; ?> />3<br>
<input type="radio" name="question0" value="D"<?php echo ($question0=='D')? 'checked':''; ?> />4<br>
</li>
Put a condition, where if any radio button misses it displays the
error message and no need to refresh the page. Inside the PHP code,
Request submit button
Request all the radio button. i.e $question1 = $_REQUEST['question1'];
check if all the values are set and not empty if(isset($question1) && !empty($question1) && $question1 !== '')
do this for all, close this condition with the error message.
This might help.

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
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.
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.
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
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>'
}

Javascript button which submits Contact Form 7 form, then redirects to URL

I'm trying to make a 'Choose Your E-mail Plan' page on my website - https://godesignweb.co.uk/e-mail-services/monthly-email/ & I'm having trouble submitting the form, and then redirecting to a specific paypal page depending on what package they've chosen.
As you can see from the website it's almost there, and depending on which package you choose, it displays a different at the bottom of the page.(which I want to contain a submit button which redirects the user).
I currently have
<input type="submit" form="email" value="send">
which is outside of the contact form 7 form, and works (it's on the Pro plan) if you click it, it submits the form.
Is there anyway I can call 3 different functions for the 3 buttons which;
1) submit the form (including checking the validation that it normally does_
2) redirect the user to a paypal page
Cheers
Updated answer:
As taken from your comment to my answer, here's what you could try if you use three different submit buttons:
1.) assign each SUBMIT button a unique name:
<!-- button in starter form -->
<input type="submit" name="submit-starter" form="email" value="Send"/>
<!-- button in pro form -->
<input type="submit" name="submit-pro" form="email" value="Send"/>
<!-- button in advanced form -->
<input type="submit" name="submit-advanced" form="email" value="Send"/>
2.) On PHP side:
if (isset($_POST['submit-starter'])) {
$plan= 'starter';
$priceMonthly= 3.99;
} elseif (isset($_POST['submit-pro'])) {
$plan= 'pro';
$priceMonthly= 5.99;
} elseif (isset($_POST['submit-advanced'])) {
$plan= 'advanced';
$priceMonthly= 7.99;
}
// create the Paypal form
?>
<form name="_xclick" action="https://www.paypal.com/de/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="me#mybusiness.com">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="item_name" value="<?php print $plan; ?>">
<input type="hidden" name="amount" value="<?php print $price; ?>">
<input type="image" src="http://www.paypal.com/en_GB/i/btn/x-click-but01.gif" border="0" name="submit" alt="Pay with PayPal">
</form>
-- previous answer --
I am not sure if I got you right because I somehow just see one (submit) button instead of three buttons so I hope my answer is not misleading or doesn't make sense in your scenario:
I assume that:
the three buttons refer to the three plans you offer
you only want to have one validation function
based on the plan selection you want to have different prices on the Paypal checkout form
The below code will only deal with the frontend control, you should definitely add a second server-based verification to make sure nobody injected/manipulated your frontend form code.
Instead of using a standard submit button, change it to:
<input type="button" onclick="formInspectAndSubmit()" value="send"/>
and add a form variable for your plan
<input type="hidden" id="input-plan" name="input-plan"/>
Your FRONTEND validation function should look like this:
function formInspectAndSubmit() {
// validation stuff ...
// retrieve the chosen plan
var plan= $('.active').prop('id');
switch (plan) {
case 'starter':
// set hidden parameters for starters
$('#input-plan').val('starter');
break;
case 'pro':
// set hidden parameters for pro
break;
case 'advanced':
// set hidden parameters for starters
break;
}
// set POST variables based on above plan and redirect to validation page; this should then forward to Paypal checkout page with price defined by chosen plan
$('#email').submit();
}

JavaScript: Clear form data after submit to self on click

Student newb here working to construct a history of my class assignments in a manner which is helpful to me for future reflection; Currently I'm doing a form assignment, which works but i'm trying to push it a touch to get the most out of the experience. My goal is that if i click the 'reset' button the form will clear regardless of if i've clicked the submit ('continue') button or not. Currently if i click the clear button before clicking the submit button, the form data will clear. But if i've clicked the continue (to submit the form to self and the form data is now sitting on in the url), the data will not clear.
this is my javascript function
<script>
/* JS to clear form */
function myFunction() { document.getElementById("myForm").reset(); }
</script>
this is the whole form part of the code:
<form enctype="multipart/form-data" action="./itc216.php" method="get">
Name: <input ="text" name="userName" value="<?php echo $userName; ?>" />
<span class="error"><?php echo $errorMessage1; ?></span><br /><br />
Amount: <input ="text" name="mealTotal" value="<?php echo $mealTotal; ?>" />
<span class="error"><?php echo $errorMessage2; ?></span><br /><br />
<input type="checkbox" name="agreement" <?php echo $agreement; ?> />
Do you agreement to the terms and conditions?
<span class="error"><?php echo $agreementError; ?></span><br /><br />
<input type="submit" name="submit" value="Continue" /> | <input type="reset" onclick="myFunction()" value="Reset!">
</form>
Reset does not clear the form, but resets the form to its default values. If you are pre-populating the form inputs with the submitted data, those are what the form will reset to. If that's the case, to clear the form, you will need to have Javascript set all values to empty.
You may need to use event delegation
$('#clearbutton').on('click', function(){
$("#myForm").reset();
});
It sounds like you want to redirect after processing the form, so that the URL is clear.
This is generally a good idea even when doing a POST, so that refreshing the page doesn't create a new entry.
I guess you aren't really processing the form yet so you don't have a way to display the entry unless it's in the URL, but that would be the correct way to do it.
You Forgot to add the id for the form in the form add
<form enctype="multipart/form-data" action="./itc216.php" method="get" id="myForm">

Categories

Resources