Save function using button in php - php

I have a doubt on the following code. My function is not called when the save button is clicked .
This is the following code for save function,
if(isset($_POST['Save'])) // If the submit button was clicked
{
echo "hgfd";
$post['ProductSegmentCode'] = $_POST['ProductSegmentCode'];
$post['ProductSegment'] = $_POST['ProductSegment'];
$post['ProductGroup'] = $_POST['productgroup'];
// This will make sure its displayed
if(!empty($_POST['ProductSegment'])&&!empty($_POST['ProductSegmentCode'])&&!empty($_POST['productgroup']))
{
echo "SAVE";
$news->addNews($post);
?>
<script type="text/javascript">
alert("Created Sucessfully..!!");
</script>
<?
}
else
{
?>
<script type="text/javascript">
alert("Enter Mandatory Fields");
</script>
<?
}
}
following is the button format in html,
<div style="width:70px; height:32px; float:left; margin-top:16px; margin-left:4px;">
<input name="Save" type="button" class="button" value="Save">
</div>

Your button is type="button"; to get the form to submit, it needs to be type="submit". Try updating it with this and it should work (also pending you form has action="post", or no action specified; the default is post):
<input name="Save" type="submit" class="button" value="Save" onclick="Save" />
Also, you're using onclick="Save" in your button. This indicates you have a corresponding JavaScript function named Save() - though, per your code examples you do not show one. I'm assuming that this is in error and can safely be removed (the value="Save" can also be removed as you only need to check isset($_POST['Save']) and not it's actual value). All changes in-place should give you:
<input name="Save" type="submit" class="button" />
If you do, in fact, have a JavaScript function named Save(), please post its code and I can revise.

use form for sending data and use type submit
<form action="" method="post">
<input name="Save" type="submit" class="button" value="Save">
</form>
and if you want to use this
<input name="Save" type="button" class="button" value="Save" onclick="Save()">
create Save() function in javascript and use ajax call for sending data.

It looks like you should change
<input name="Save" type="button" class="button" value="Save" onclick="Save">
to a summit button.
<input name="Save" type="submit" class="button" value="Save">

Related

A way to determine ID of form during POST method

I was wondering if it was in any way possible to be able to determine the ID of the form that was posted on PHP?
<form id="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
In the enroll.php
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
P.S: I'm not too sure on how i would do this on PHP. Thank you in advance
Post does not use the ID of the element, rather the name, so instead of your current form, you could use;
<form name="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<input name="formid" value="ES1A" /><!-- This holds your form id so you can use this -->
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
And in the PHP;
if (isset($_POST['ES1A']) // Unsure if form itself will be posted with the submit
{
// This is set as it uses the name of the element
$formid = $_POST['formid']; // Get the ID of the form from the element passed in
}
If form's name attribute does not work you can always set name for button:
<button name="ES1A" type="submit" class="btn btn-primary">Next Step</button>
Or:
<input name="ES1A" type="submit" class="btn btn-primary" value="Next Step">
PHP part should be the same as you already have:
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
an alternative is to use hidden input
<input name="ES1A" value="formid" type="hidden" />

Multiple button on a single form but only one submit button?

I want to pass data from a single form, but it has another 2 button to add and delete questions, the problem is when I click on any of problem or create a question problem, the form problem to the action url.. I just want when I click on the Submit button then action is activated.
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
The default type of a button element is submit.
You can specify type="button".
It is better practise, however, to give it a name and a value so that you can perform whatever task you want it to do server side should the JS fail, and then call event.preventDefault() in the event handler function.
I think I have found the solution instead of using
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
I have to use this
<form method='POST' action='scripts/generateguide.php'>
<input name='create question' class='addP' type="button" value="Submit">
<input name='delete' type="submit" value="btton" class='deleteCon'>
<input name='createGuide' type="submit" value="Submit">
</form>

PHP submit refreshes the page and takes the submit's id as a parameter

This is my problem - when I hit the submit button it is supposed to execute some code. Unfortunately all it does is refresh the page at a weird url using the submit's ID and value.
File is called Q2C.php.
This is the code:
<?php
if(isset($_POST['submit'])) {
$arraylength = count($selectedQuotes);
for($i = 0; $i < $arraylength; $i = $i + 1) {
$supplierQuoteID = $selectedQuotes[$i];
$updateSQL = "UPDATE supplierquotes SET `Selected` = 1 WHERE `SupplierQuoteID` = '$supplierQuoteID'";
$updateQuery = mysql_query($updateSQL);
}
}
?>
<div class="buttons">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</div>
It redirects me to this url: (path)/Q2C.php?submit=Create+Q2C
How can I stop it from redirecting me here, and instead just perform what is in the if isset statement?
You need to put your buttons into a form.
<form method="post">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</form>
You should consider changing your second submit button to a cancel button.
I don't see a <form> in your HTML, but you need to be using POST, not GET
<form method=post>
You have not submitted your form completely in the above code, but it is clear from the redirected url that you are using GET as post method of the form. And in your isset check you are using $_POST. Change the form method to post then it will work fine.
You need to specify method. By default its get
<div class="buttons">
<form action="Q2C.php" method="post">
<input type="submit" name="submit" id="submit" value="Create Q2C" />
<input type="submit" name="cancel" id="cancel" value="Cancel" />
</form>
</div>
You should probably use ajax to submit a form if you don't want to refresh the page. Here is the same code you can refer to:
$.ajax({
type: "GET",
url: "abc.php",
data: "name="+form.name.value,
success: function(rs)
{
// do success stuffs here
}
});
Hello am not sure if i get your problem correct its like You want Submit a form to Q2C.php with out a form attribute called "Method" if u dont set the method at all, the form will use the GET method as default..... but from your code it seems the form is using get to submit to Q2C.php.... and you are saying (isset($_POST['submit'])) which is wrong because the Submit button was never set.. what u need to do is change set the attribute method of the form to "post" method and try running the code again... your form should look like this...
.........

Why won't my return work?

I only just started learning JS like 5-10 minutes ago, I was told by someone to try and create a basic validation feature, however it doesn't seem to work as wanted. It checks if field is empty, that part works. But checking if it's got something in it and carrying on running the code doesn't.
my form:
echo '<form action="index.php?action=getHashedText" method="post" name="formHash">
<br/><textarea name="text" rows="4" cols="50" placeholder="Add your text/pharse/word which you want hashing here." autofocus></textarea><br/>
<button type="button" name="button" onclick="return validate();">Hash</button>';
function validate():
<script>
function validate() {
with (window.document.formHash) {
if (formHash.text.value === "") {
alert('Field is empty!');
return false;
} else {
return true;
}
}
}
</script>
The problem is you are using a button that is not submitting the form anyway (regardless of your js), change this dom:
<button type="button" name="button" onclick="return validate();">Hash</button>
To this: Fiddle
<input type="submit" name="button" value="Hash" onclick="return validate();" />
Or you can just add type="submit" on your <button> (HT #RocketHazmat): Fiddle
<button type="submit" name="button" onclick="return validate();">Hash</button>
Or you can just remove the type on your <button> all together as the default type is submit (HT #FabrícioMatté): Fiddle
<button name="button" onclick="return validate();">Hash</button>
Also, slightly off topic but I would get in the habit of avoiding putting javascript onclicks directly on your elements. You can create listeners instead: addEventListener

2 submit buttons on form - need one to open a new window

Using a form with a blank action - action="".
I have 2 buttons on the form that do different things. one to submit/save the info, the other to open an output sheet:
<input type="submit" name="SubmitSave" id="SubmitSave" value="Submit / Save" onClick="this.form.action='PA_Monitorcall.php'; this.form.submit()" />
<input type="submit" name="EmailDetails" id="EmailDetails" value="Email" onClick="this.form.action='OutputSheetPA.php'; this.form.submit()" />
I need the output sheet to open in a new window, but can't have this in the form header details, it will need to go in the code for the button above. Any ideas?
Cheers!
onClick event of both submit buttons, call a javascript function, which would toggle the 'target' attribute of the form tag to '_blank' or '_parent'/''.
with this new value for 'target' attribute your post would be submitted in a new window/tab
<form target="" action="" method="post">
<input type="submit" value="Same Window" onClick="ChangeTarget('same')" />
<input type="submit" value="New Window" onClick="ChangeTarget('new')" />
</form>
function ChangeTarget(loc) {
if(loc=="new") {
document.getElementById('form_id').target="_blank";
} else {
document.getElementById('form_id').target="";
}
}
Use type="button" instead. Your onClick already calls submit, so you don't need them to be submit inputs.
You can name the two inputs with the same name and then check the value of that inputs.
<form action="">
<input type="submit" name="submit" id="SubmitSave" value="Submit / Save" />
<input type="submit" name="submit" id="EmailDetails" value="Email" /></form>
</form>
And in the php file:
<?php
if ($_POST['submit'] == 'Submit / Save')
// save the form input
elseif ($_POST['submit'] == 'Email')
// do other stuff
...

Categories