i have form with submit button ,when i submit the form the value of the button should insert into the databse and change text of button to inserted.if the insertion is not possible(violates constraint) change the text of button to not inserted
<html>
<form action="" method="post">
<button type="submit" name="button1" value="one" id="bttn">CLICK</button>
</form>
</html>
<?php
if(isset($_POST['button1']))
{
$a=$_POST['button1'];
$con=mysqli_connect("localhost","PROJECT8","PROJECT8");
mysqli_select_db($con,"PROJECT8");
$rs=mysqli_query($con,"insert into ruff values('$a')");
if($rs)
{
//if data is inserted i need to change the button text to "inserted"
}
else
{
//change the button text to "not inserted"
}
}
?>
i searched about and it shows you need to use AJAX i am a newbie in AJAX
the complete code will be very helpfull
Try below code in your case,
if($rs) {
echo "<script>
document.getElementById('bttn').innerHTML = 'inserted';
</script>";
}
else {
echo "<script>
document.getElementById('bttn').innerHTML = 'not inserted';
</script>";
}
Related
I have a PHP form set like this
<?php if (isset($submitted)) {
$output = checkData();
if (!is_null($output))
{
echo '<script type="text/javascript">alert("' . $output . '"); </script>';
}
else
{
createMeeting();
echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>';
header('Location: index.php');
}
} else { ?>
<center>
<form method="POST">
...
<input type="submit" name="submitted" value="Create Meeting">
</form>
<?php
}
?>
When I run it through a PHP code checker (codechecker website), no errors are returned. However, when I click on the submit button, the isset($submitted) code never seems to be executed (I've tested this by adding some echo statements in that section of the code).
If I click on the submit button, the form is cleared, so something is happening. I've put a number of different actions in, but the code is still not hit.
This if (isset($submitted))... should be if (isset($_POST['submitted']))...
You can check if the form is submitted using:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Or check if an individual element of the form has been submitted checking if the name is in the post array using:
if(isset($_POST['submitted']))
im trying to make something, and now i came to the part where i created a button that should lock the topic, but i don't know how to make it work like submit form, when i press it it should lock the topic, but nothing happens:
echo '<br><div class="ticket_info">';
if($stanje == 0 ) {
echo ' <button class="lockticket" name="lockticketbutton">Zatvori</button></a> ';
}
else if($stanje != 0 ) {
echo ' <button class="lockticket" name="unlockticketbutton">Otvori</button></a>';
}
echo '
Postavio: '.$output['Postavio'].' --- ['.$output['Naslov'].']
</div>
</br><div class="ticket_info2">
'.$output['Text'].'
</div>
';
And this is what it should output:
if(isset($_POST['lockticketbutton']))
{
$id = $_GET['id'];
$query = mysqli_query($con, "UPDATE `Dashboard` SET `Status` = '1' WHERE `ID` = '$id'");
if($query)
{
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else
{
header('location: dashboard.php');
}
}
you can submit the button by using following code
<form action ="your php file name" method ="post">
<button type= "submit" class="lockticket" name="odgovoritiket" >Otvori</button>
</form>
To use a button as Submit you need to put it into form.
Note: according to w3schools:
If you use the <button> element in an HTML form, different browsers may submit different values. Use <input> to create buttons in an HTML form.
I have been trying to get the PHP code to submit an email to a mysqlDB, but for some reason it is not working:
This is the form code in the HTML
<form class="header-signup" action="registration.php" method="post">
<input name="email" class="input-side" type="email" placeholder="Sign up now">
<input type="submit" value="Go" class="btn-side">
<p class="hs-disclaimer">No spam, ever. That's a pinky promise.</p>
</form>
For the PHP, I did the following (DB connection infos set to xxxxx):
<?php //start php tag
//include connect.php page for database connection
$hostname="xxxxxx";
$username="xxxxxx";
$password="xxxxxx";
$dbname="xxxxxx";
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
//Include('connect.php');
//if submit is not blanked i.e. it is clicked.
If(isset($_POST['submit'])!='')
{
If($_POST['email']=='')
{
Echo "please fill the empty field.";
}
Else
{
$sql="INSERT INTO MailingList (MAIL) VALUES('".$_POST['email']."')";
$res=mysql_query($sql);
If($res)
{
Echo "Record successfully inserted";
}
Else
{
Echo "There is some problem in inserting record";
}
}
}
?>
Do you know what might be the problem?
The php file is in the same folder than the webpage.
Thanks for your time
Regards
$_POST['submit']
does not exist, you have to specify the name for the submit button
<input type="submit" name="submit"........>
Please try this
You could also use this conditional for a POST request
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
And check the input with a var_dump($_POST); to see if the value exists in the array.
This is only if you're expecting one form. If you want multiple forms on the page you could make use of naming your submit button in the HTML code
<form class="header-signup" action="registration.php" method="post">
<input type="submit" name="action1" value="Go" class="btn-side">
</form>
You could also use this if you set an name on the submit button
if(isset($_POST['action1']))
{
var_dump("hit");
}
I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})
I'm having a rather irritating issue when trying to submit data from a html button tag via POST to a PHP processing script.
Here's my code...
(1) HTML form:
<form id="pub-form1" method="post">
<button type="submit" name="All" value="true">All Publishers</button>
<button type="submit" name="Current" value="true">Current Publishers</button>
<button type="submit" name="Users" value="true">User Priveleges</button>
</form>
(2) jQuery script to handle POST request:
$(document).ready(function () {
$('#pub-form1').submit(function (e) {
e.preventDefault();
$('#results').contents().remove();
var formData = $(this).serialize();
$("input").prop("disabled", true);
request = $.post('VRC_PublishersProcess.php', formData, resultsMessage);
request.fail(function() {
$('#results').append("<span id=\"reply\">Your search failed for an unknown reason. Please try again in a few minutes.</ span>");
$("input").prop("disabled", false); });
function resultsMessage(data) {
$('#results').append(data);
$("input").prop("disabled", false);
}
});
});
(3) PHP processing script:
//All Publishers
if(isset($_POST['All'])) {
if(!empty($_POST['All'])) {
$result = $members->selectAllMembers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
//Current Publishers
if(isset($_POST['Current'])) {
if(!empty($_POST['Current'])) {
$result = $members->selectCurrentPublishers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
//Users
if(isset($_POST['Users'])) {
if(!empty($_POST['Users'])) {
$result = $members->selectUserPublishers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
I'm not quite sure what the cause of the issue is. No errors were thrown by javascript or PHP; the page remains blank with no results.
The jQuery function seems to be working correctly. I've use that particular several times on other forms and it handles the POST request properly. I was even able to trigger a failure on it; it responded correctly.
The PHP processing script also appears to be functioning properly. Again, it derived from other scripts that have successfully processed POST requests from the same page.
My suspicion is focused on the HTML form itself. These are my theories: (1) The button isn't submitting "itself." In other words, since there is no text or other forms of input, there's really nothing to submit. (2) For some reason the name is not corresponding to what my PHP script is looking for. For instance, if "All Publishers" was submitted, my PHP script is looking for $_POST['All']. But for some reason, that request isn't saved under that identifier in the PHP superglobal array.
My goal is to provide the end user with three options of displaying data. Right now, that isn't working.
Any feedback is appreciated.
Final Solution:
<div id="pub1">
Other
<div id="displayselect" method="post">
<form id="select1" method="post">
<input type="hidden" name="All" value="1" />
</form>
<form id="select2" method="post">
<input type="hidden" name="Current" value="1" />
</form>
<form id="select3" method="post">
<input type="hidden" name="Users" value="1" />
</form>
<button type="submit" name="All" form="select1">All Publishers</button>
<button type="submit" name="Current" form="select2">Current Publishers</button>
<button type="submit" name="Users" form="select3">User Priveleges</button>
</div>
</div>
Obviously, appropriate modifications were made in the jQuery...
Figured it out,
jQuery.serialize didn't work out on the button (http://api.jquery.com/serialize/) it only works on input, textarea, select so I tried switching your HTML button elements into <input type="submit" ... but that didn't work out as well because jQuery ignores the submit tags when serializing, I tried to add an <input type="hidden" name="test" value="1" /> and checked out what it serialized was test=1 so you'd have to change your form a bit.
jsFiddle for testing: http://jsfiddle.net/4D8Nz/