how to get the text of submit button in php - php

Hello im having problem of getting the text on submit button .
what im going to do is use if statement to use a single button to insert and update records. ive found a code in this site but it doesn't give me desired output
foreach($_POST as $name => $content) { // Most people refer to $key => $value
echo "The HTML name: $name <br>";
echo "The content of it: $content <br>";
}
another problem how do i change the text of the button control using javascript
the code below doesn't work
<script type="text/javascript">
$(document).ready(function () {
//Attach button clicke event hanlder
$('.btnedit').click(function () {
var title = document.getElementById('btnsubmit');
title.innerHTML = "Update";
});
});
</script>
this is the code for button
<input type="submit" name="submit" id="btnsubmit" class="btn btn-default" value="Save" />

Give name and value to your submit button
<input type="submit" name="submit" value="SUBMIT" />
The echo in foreach will be
The HTML name: submit
The content of it: SUBMIT
To get the value without foreach(), you should do like this.
echo $_POST['submit'];// will echo SUBMIT
^ name of your submit button.
See demo HERE

Related

PHP form button redirects (is changing the URL) even if I don't want to

So, I have a button in a form.
<form>
<button onclick="alert('<?php echo $value ?>')">Click me!</button>
</form>
$value = "1.png"
When I press it changes the url like this:
Initial:
index.php?id=82
After I click:
index.php?
As you can see, I want the button to only show an alert, not to change the URL
Full code:
<form>
<label>Title</label><br>
<input type="text" value="<?php echo $title ?>"><br><br>
<label>Description</label><br>
<textarea rows="5" maxlength="120"><?php echo $desc ?></textarea><br><br>
<div>
<?php for($k = 0; $k < count($images); $k++) { ?>
<div>
<img src="<?php echo $images[$k] ?>">
<button onclick="alert('<?php echo $images[$k] ?>')">Click me!</button>
</div>
<?php } ?>
</div>
</form>
PS: I'm not sure what's the problem but I think is the form
There are at least two ways of achieving this:
html:
<button type="button" onclick="alert('<?php echo $images[$k] ?>');">Click me</button>
<button onclick="alert('<?php echo $images[$k] ?>');return false;">Click me!</button>
The first option I think would be best if the only thing you want to achieve is to alert a text.
The second option might be better if call a function when you click on the button and want different responses:
<button onclick="return foo('<?php echo $images[$k] ?>');">Click me!</button>
in javascript:
function foo(image) {
//If image is img1 then update the page
//If any other image, don't update the page
if (image == 'img1') {
return true;
}
return false;
}
When you fail to specify the type of a <button> element, the browser will imply that it is a submit type button. Thus, when you press the button, your browser is submitting the form with it.
You should either,
a) Specify a type for the button other then submit, or
b) Return false in the javascript code to run, to prevent the click event from bubbling.

getting selected radio button value in a popup

I have a submit button named "details:, and a table which had radio buttons in each rows.
On selecting radio button and then clicking on my details button I want a popup to appear.
I have used something like this: I am not sure if the code I tried is really pathetic, but plz help me.
The form I used is GET.
This is my radio button:
<input type="radio" name="ID" value="<?php echo $id; ?>" class="radioValue" />
This is my submit button:
<input type="submit" name="details1" value="Details" id="btndetails" class="btn" />
<div id="notice">
<div class="pop">
<?php
if(isset($_GET['details1']))
{
if(isset($_GET['ID']))
{
$n=$_GET['ID'];
echo$n;
//do some sql queries with the id
}
else echo"Fail";
}
?>
</div>
</div>
I have used jquery to ensure the clicks:
(function($)
{
$(function()
{
$('#btndetails').bind('click', function(e)
{
// Prevents the default action to be triggered.
if($('.radioValue').is(':checked'))
{
e.preventDefault();
$("#notice").css("display", "block");
}
else
{
<?php echo"Please select the radio button";?>');
}
});
});
})(jQuery);
the div notice is display:none;
Currently on clicking the radio button + details button i am getting a popup with msg "fail". How can I get my radio button value in the pop up? Is my method wrong? I need to get the radio value in php so that i can use it to retrieve my DB.
I dont want to use any plugins for popups. I want it only by using php,html, css .
Please help me.
This might be your problem:
<input type="submit" name="details1" value="Details" id="btndetails" class="btn" />
<div id="notice">
<div class="pop">
<?php
if(isset($_GET['details1']))
{
if(isset($_GET['ID']))
{
$n=$_GET['ID'];
echo$n;
//do some sql queries with the id
}
else echo"Fail";
}
?> // the first problem I found is here
</div>
</div>
also check your URL are you getting id parameter in query string

How to do different javascript actions with two buttons in one form?

I'm trying to do two different javascript actions with jquery for my php form which has two submit buttons: 'save' and 'next'. The idea is that both button submits form that saves data into db, but while 'next' goes through client-side validation and progress further, the 'save' just skips validation, returns true and the user stays on the form.
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>
I already managed to succeed when user clicks either 'save' or 'next' after reload, but if user clicks 'next', launches validation and submit returns false, he cant click 'save' and ignore validation anymore. What might be the cause of this?
$(function() {
//Lets skip the whole thing if save is clicked
$('#save').click(function() {
$('#form').submit(function() {
return true;
});
});
$('#next').click(function() {
$('#form').submit(function() {
var invalid = 0;
//A lot of crazy validation, if some invalid stuff then increment increment invalid
if(invalid > 0) {
return false;
}
else {
return true;
}
});
});
});
I think I would do something like this :
$(function() {
//Lets skip the whole thing if save is clicked
//Actually, no binding is needed as the button is already a submit button
//Thx to Ocanal
$('#next').click(function() {
var invalid = 0;
//Validation process
if(invalid) {
$('#form').submit();
} else {
return false;
}
});
});
Why would you skip validation, especially if it so crazy ? Anyway the problem is here
$('#form').submit(function() { ... }
this doesn't overwrite the submit event handler, this ADDS a function to it. Therefore if you first click next then save , the function you defined for next will still be triggered when clicking on save.
While it's not clear with the notation, it's quite logic : that's what allows you to "add" action to your documentReady event from wherever you wish, not only from a central place.
You can use a different type for your non-submit button.
You'll want something like this:
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="button" name="next" id="next" class="next" value="next"/>
</form>
I think what's happening to you now is that both buttons are acting as your "submit" button, so the form is trying to submit, regardless of which button you're clicking, or what functions you've added to the EventListener.
First of all, Java Script is created every time when refreshing the page. It's important to understand that.
Now, if you click on the button 'Save' of type submit, your information will pack up in the form packet and sent to the server. The submit action requires reloading of the page(!).
Therefore, if you want to keep values ​​in fields (if that's what you want) you may use PHP.
Using PHP is not complicated. I combine the code inside the <body> tag and before the <form> tag.
The code checks whether there is a value in the 'Save' field of $_POST variable, if true, we will save the received values.
And then, I present the values ​​using variable access <? = $name ?>. That's it.
<?php
$name = "";
$credit = "";
if(isset($_POST['save'])) {
$name = $_POST['name'];
$credit = $_POST['creditCard'];
}
?>
<form id="form" name="form" method="post" action="good.php">
<input type="text" name="name" value="<?=$name ?>" />
<input type="text" name="creditCard" value="<?=$credit ?>"/>
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>

Get data from submit button, display text file , clear text box

My need is bit more complicated for me,
but for many it will be easy..
Here is what i want,
in my form i have 3 objects, Textbox, Submit Button, and Display area.
Users have to enter some details in Text Box and to Press Submit button,
The details have to be displayed in the Display Area (Display Box) which is below.
After Displaying the details the text box must be empty. Users cannot or should not edit the details in display area.
Here is what i had tried,
Enter Text , Click Submit button- The data will be saved in Text file1
Open the Text file2 in appending mode and add Text file1 into Text file2
Display Text file2 , Clear Text file1 (for Next Use)
Its pretty Basic HTML + Javascript . Anyway here the code :)
<html>
<head>
<script>
function display()
{
document.getElementById("displayarea").innerHTML = document.getElementById("myinput").value;
document.getElementById("myinput").value = "";
}
</script>
</head>
<body>
<input type="text" name="myinput" id="myinput">
<input type="button" value="Submit" name="submit" id="submit" onClick="display()"/>
<div id="displayarea">Sample Text. This data will disappear if you press button</div>
</body>
</html>
//view.php
<?php
if (isset($_POST['submit'])
{
$text = $_POST['text1'];
echo $text;
echo "<br />";
}
echo "<form action= '' method='post'>
Text Box: <input type='text' name='text1' />
<input type='submit' name='submit' />
</form>";
?>

disabling user input php/html form

I have a HTML form which when the user clicks submit, all information is sent to email via a php script using the POST method. What i want to do is disable all user input after the submit button is clicked or grey out all text box input fields. How can i go about doing this?
the code for the submit button at the moment looks like this:
<input type="submit" value=" Continue " style="width:200px;height:40px">
Add below to each HTML input element you want to disabled. Since you have not mentioned I assume that you can use PHP for this.
<?php if(isset($_POST['submit'])) echo 'disabled="disabled"'; ?>
If you are using AJAX, when the event is fired, inside particular event
$("input").prop('disabled', true);
Onclick of submit button make all input to disabled
$("input").attr('disabled', true);
Since whether mail has been sent is server side processing, its better if you handle this from server end than from client side like jQuery and javascript :
$form_state = ($is_mail_sent)?"":"disabled='disabled'";
<input type="submit" <?php echo $form_state ?> value=" Continue " style="width:200px;height:40px">
without db interaction you can't do this out, have a flag in db table. Set the flag 1 if user submit a form, by default 0. Based on the flag value you can disable the form.
if(mailsent_status_of_the_user == 1){
$status = "disabled='disabled'";
}else{
$status = "";
}
<input type="submit" value=" Continue " <?php echo $status; ?> >
By using jQuery : $("input").prop('disabled', true);
But in pure Html is not possible without refreshing page !
Edit :
Javascript :
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(e) {
$("#yourFormID").submit(function() {
$("#theInputToDisable").prop('disabled', true);
//Or to disable all inputs
//$(this).children("input").prop('disabled', true);
//$(this).children("input").attr('disabled', 'disabled');
return false;
})
});
</script>
HTML
<form id="yourFormID">
<input type="text" />
<input type="submit" value="Send"/>
</form>

Categories