Disable another button once it has successfully submitted the form - javascript

I have a form with 2 buttons: Approve and Reject.
<input type="button" name="Approve" value="Approve" onClick="location.href='approve.php?user=<?php echo $row['icnum'];?>&states=1'">
<input type="button" name="Reject" value="Reject" onClick="location.href='approve.php?user=<?php echo $row ['icnum'];?>&states=2'">
I want to disable the "Reject" button once the "Approve" button is clicked. So, the user cannot reject the form again after it has been approved.
I have tried several attempts, like:
<input type="button" value="Send" onclick="javascript=this.disabled = true; form.submit();">
But this code does not work for me. Even if it's actually working, the button is just disabling itself, not the other button.
I'm using Chrome. Any idea on how to solve this?
EDIT: This is my process form in php.
<?php session_start();
include('../include/dbconnect.php');
$user = $_GET['user'];
$states = $_GET['states'];
if($user){
$approve = "UPDATE student SET status='$states' WHERE icnum='$user'";
$result = mysql_query($approve) or die(mysql_error());
if ($result) {
?>
<script type="text/javascript">
window.location = "index.php"
</script>
<?php }
}
?>
What can I edit into these codes if the action is ran in the server-side?

Remove the javascript= from OnClick. You only need javascript: (colon not =) for anchors' hrefs
<input type="button" value="Send" onclick="this.disabled=true; formName.submit();">

In jQuery:
$('input[name=Approve]').click(function () {
$('input[name=Reject]').prop({disabled: true});
});

Try below
<asp:Button ID="cmdSubmit" runat="server" Text="Submit" onclick="btnSumbit_Click" onclick="btnSumbit_Click" OnClientClick="this.style.display = 'none';"/>

Related

Ajax Load Data Button and Links not Working

I have a problem with jQuery Ajax load. On message.php, it shows messages and it has copy and delete link. When I click on delete, it deletes the message from the mysqli database. But when I use ajax.load('message.php') to load the message.php page, the delete link does not work at all.
Is there a way for me to click hyperlink or button from the ajax.load()?
Here is my code
<script>
$(document).ready(function() {
$('#loadBtn').click(function() {
$('#messageDiv').load('message.php');
});
});
</script>
<div id="messageDiv"></div>
Here is my message.php
<html>
<body>
<form id='delete'>
<input type='hidden' name='id' value='$id' id='id'>
<button id='delete'> Delete </button>
</form>
</body>
</html>
<?php
$conn_database = mysqli_connect('localhost','root','content','message');
$id = mysqli_real_escape_string($conn_database, $_POST['id']);
$sql_delete = "DELETE FROM message WHERE id = $id";
mysqli_query($conn_database, $sql_delete);
?>

return method for form submission in javascript

developers i create one page where it fetches data from the registration page. Each data row I put add and unfriend button (disabled). Once the user clicks add, the prompt box appears to ask the user to enter a subject and click ok. After click ok,it insert in another database table.while the unfriend button will be able to click. Here my problem is once click add button the prompt appears and after click ok, the data does not insert into the database. If I click unfriend button it inserts into the database. I want the data submitted whenever the user clicks the add button. I think it's because this form has two submit buttons but I don't know how to distinguish between the buttons.Moreover,in javascript i put return false and true follow some tutorials.may i know when i should use return false and true?. Here is the code:
<?php
session_start();
$mysqli=new MySQLi('127.0.0.1','root','','learning_malaysia');
$sql = "SELECT * FROM tutor_register INNER JOIN tutorskill ON tutor_register.register_ID = tutorskill.register_ID";
$result= mysqli_query($mysqli,$sql);
?>
<html>
<script>
function myFunction(form){
var subject = prompt("Please enter Subject that want to study");
form['add'].value="request sent";
if (subject != null){
form['subject'].value= subject;
form['btn'].disabled=false;
form['add'].disabled=true;
return false;
form.submit();
}
return true;
form['add'].submit();
}
function unfriend(form){
form["add"].disabled=false;
form["btn"].disabled=true;
form["add"].value="Add friend";
return true;
form.submit();
}
</script>
<body>
<?php
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
$register_ID=$row["register_ID"];
?>
<form method="post" id="form" enctype="multipart/form-data" autocomplete="off">
<input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
<input type="hidden" id="subject" name="subject" data-uid=<?php echo $_SESSION['sid'] ;?>/>
<td><input type="submit" onclick="return myFunction(this.form);" name="addfriend" data-type='addfriend' id="add" class="btn" value="add" />
<input type="submit" value="unfriend" id="btn" onclick="return unfriend(this.form);" disabled /> </td> </form>
<?php
}
}
?>
<?php
if(isset($_POST['subject']) and $_POST['id']) {
$user_id = $_SESSION['sid'];
$friend_id = $_POST['id'];
$status = "yes";
$subject=$_POST['subject'];
$sql="INSERT INTO friends(user_id,status,subject,friend_id)" ."VALUES('$user_id','yes','$subject','$friend_id') ";
if($mysqli->query($sql)=== true) {
$_SESSION['status']="yes";
$_SESSION['friend_id']=$friend_id;
$_SESSION['user_id'] = $user_id;
} else {
}
}?>
</body>
</html>
Your return statements terminate the function; no code after the return will execute, so your form.submit() calls never happen. In your friend function, because you're returning false and your onclick has return friend(...), you're cancelling form submission, and the form is never submitted (not by your code, and not by the browser), In unfriend, though, you're returning true, so although your code doesn't submit the form, the browser does.
If you want to submit the form programatically, put those form.submit calls before the return, and return false so the browser doesn't also submit the form.

Javascript button click change / create PHP variable on same page

I have searched stackoverflow for the past hour and have found similar questions but attempting the solution has given me only errors - it may be as I am trying to load it on the same page.
I have tried saving the value to a cookie in JS then loading it in php, also attempted through ajax, etc.
From what I've read it can't just be done within as PHP is server side and Javascript is browser side which I get.
I currently have a .html page with a form that sends values through POST:
form.html
<form id="calc" form name="calculator" method="post" action="/calculate.php">
The solution is not with the form, I can't have hidden fields as I need to user input. I could have a radio box in the form yes and then get the value that way, but that's not my question. My question is how I can get the user's input and turn it into php on the page the form redirects to calculate.php:
calculate.php
<?php
if(isset($_POST['submit'])){
$x= $_POST['value'];
echo "your something:";
echo $x;
?>
Click button below for option one to do this, or click two for that, or three for something else...
<button id="one"> one </button>
<script>
$(document).ready(
function() {
$("#one").click(function() {
make php value 1
});
</script>
use php value from click
So the user will already have clicked submit. Then be redirected onto the .php page with their inputs, they can then choose button 1, 2 or 3 for example through javascript on calculate.php NOT the form.
My problem is I need to know on the same page - calculate.php, which button has been clicked in php. So that I can use their input and do different things with it depending on which button.
Is it possible to get a value to be set or create one from a button click on calculate.php itself with the javascript button producing a php value on the same page? Does it need to be loaded from an external.php page through ajax? Is there any other way?
PHP Option:
<?php
if(isset($_POST['submit'])){
$x= (isset($_POST['one'])) ? $_POST['one'] : '';
$y = (isset($_POST['two'])) ? $_POST['two'] : '';
$z = (isset($_POST['three'])) ? $_POST['three'] : '';
echo "your something:";
echo $x;
echo $y;
echo $z;
?>
<form name="calculator" method="post">
<INPUT TYPE="RADIO" NAME="one" VALUE="1"> ONE
<INPUT TYPE="RADIO" NAME="two" VALUE="2"> TWO
<INPUT TYPE="RADIO" NAME="three" VALUE="3"> THREE
<input type="submit" value="Submit">
</form>
JS mode:
if(isset($_POST)){
$value= (isset($_POST['value'])) ? $_POST['value'] : '';
$previous = (isset($_POST['previous'])) ? $_POST['previous'] : '';
//play with value and previous one
$x = //your algorithm;
echo "your something:";
echo $x;
?>
<form name="calculator" method="post">
<button type="button" value='1'>ONE</button>
<button type="button" value='2'>TWO</button>
<button type="button" value='3'>THREE</button>
<input name='value' type='hidden' value=''>
<input name='previous' type='hidden' value='<?= (isset($x)) ? $x : ''?>'>
</form>
<script>
$(document).ready(
function() {
$("button").click(function() {
$('input[name=value]').val($(this).val());
$('form').submit();
});
</script>
use php value from click

How do I run a PHP code when user submit data with html

I wrote a piece of code, when the user click on submit button it send a string to PHP and then my code will run a Mysql query (based on the submitted string) and then using file_put_content it will upload the mysqli_fetch_array result to the file.
All I want to do is without refreshing the page it submit the value to php form and run the code then show Download From Here to the user.
How should I do that using javascript or jQuery ?
if(#$_POST['submit']) {
if (#$_POST['export']) {
$form = $_POST['export'];
echo $form;
$con1 = mysqli_connect("localhost", "root", "", "test_pr");
$sql2 = "SELECT email FROM `my_data` WHERE email LIKE '%$form%'";
$result2 = mysqli_query($con1, $sql2);
$rows = array();
while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
$rows[] = $row['email'] . PHP_EOL;
}
$nn = implode("", $rows);
var_dump($rows);
echo $nn . PHP_EOL;
$file = fopen("export.csv", "w");
file_put_contents("export.csv", $nn);
fclose($file);
}
}
?>
<html>
<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method=post>
<input name="export" type="text" value="example" /> Export Address<br/>
<input name="submit" type="submit" value="submit" />
Download From Here
</form>
</html>
Assuming you know how to include jquery, you would first bind a submit handler to the submit button, (I've added an id to make it easier) and prevent the default submit action. Then add an AJAX post request to the handler. This will post to your php file. Have that file echo out your link, then have the ajax callback function append it to the desired element. Something like this:
<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="form1" //Add an id to handle with >
<input name="export" type="text" value="example" /> Export Address<br/>
<input name="submit" type="submit" value="submit" />
Download From Here
</form>
<script>
$("#form1").submit(function (event) {
event.preventDefault();
$.post("//path of your php file here",{inputText: $("input[type='text']")},function (returnedString) {
$("#whereToPutReturnedString").append(returnedString);
});
});
</script>
Also, if you want to just show the link when the button is clicked, do the following:
<script>
$("input[type='submit']").submit(function () {
$("#idOfElementToPlaceLink").append("Your anchor text");
});
</script>
or you could just have it hidden with css or jquery and do $("#theId").show();
If you need more help, just holler!

Two onclick on one element

I have a simple code to delete data from the database,and I want a warning message to appear before deleting. the code works fine when I put one onclick and when I put two only the first onclick works.
and I need to onclick one for the warning message and the other for delet.php page.
<form><input type="button" value="delete" onclick="return confirm('Really delete?');" onClick='window.location.href="delete.php?id= <?php echo $id; ?>"' ></form>
This is how I would handle it:
<input type="button" value="delete"
onclick="confirmDelete('<?php echo $id; ?>');">
Then elsewhere in the page:
<script type="text/javascript">
function confirmDelete(id) {
if(confirm('Really delete?')) {
window.location.href= "delete.php?id=" + id;
}
}
</script>
Notice I removed the <form> element. If this is the only context this is in, the <form> element is unnecessary since you're not actually filling out a form, just clicking a button. If there is more context you didn't include in your question, then it might be relevant to that.
Try using this with an if clause:
<form>
<input type="button" value="delete" onclick="
if(confirm('Really delete?')) {
window.location.href='delete.php?id=<?php echo $id; ?>';
}
else {
return false;
}
">
</form>
You can only use one onClick. But there's a way to do what you want in only one ;)
<form>
<input type="button" value="delete" onclick="
if(confirm('Really delete?')){
window.location.href=\"delete.php?id= <?php echo $id; ?>\"
}else{
return false;
}" />
</form>

Categories

Resources