PHP POST is not filled - php

I am having a problem with a website I have to create. When I call the POST with the right key I dont get the value back. I think the error is that my POST does not get filled.
$id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int) $_GET['id'] : 0;
This line is on top of the coding and gives the right id back, I checked that via an echo.
<form method="POST" action="Index.php">
<input type="hidden" name="Entryid" value="<?php echo $id; ?>">
<a type="submit" name="delete" class="btn btn-success" href="Index.php" data-toggle="modal" data-target="#myModal" style="cursor: pointer;">Löschen</a>
</form>
Here I put the value of id into the key "Entryid" and switch to "Index.php" when I click on a button.
Inside of "Index.php" I try to get the value like that.
$id = isset($_POST['Entryid']) && is_numeric($_POST['Entryid']) ? (int) $_PST['Entryid'] : 0;
Here the value of id is probably not set and if I check it via echo the output is the 0.
Where do you think my error is?

I think it's because you're using an <a>-tag as the button, and not an input.
So change this line:
<a type="submit" name="delete" class="btn btn-success" data-toggle="modal" data-target="#myModal" style="cursor: pointer;">Löschen</a>
to this:
<input type="submit" name="delete" class="btn btn-success" style="cursor: pointer;" value="Löschen" />
The error occurs, because you've made an <a> with a href, - which means that you're not submitting a form to the destination, - but instead just linking it (with a regular get-request without any additional parameters). This page explains a bit about it (the section about 'The Method Attribute').
But as Ali said. Try on your index.php-page, to write:
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
And see what you get, to see if your variable is defined.

Don't use an a to submit. That doesn't process the form. It just links to the page. Use:
<input type="submit" ...
and style it as needed.
The type of an a is not for how it processes a form, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a.
Specifies the media type in the form of a MIME type for the linked URL. It is purely advisory, with no built-in functionality.

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.

use $_GET['']; value inside $_POST[''] and submit it to the same page and display in the same page

i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.

Issue with PHP button activation

I want to show a button in my page only if a certain condition is met.
Also i want to run a query (DELETE QUERY) when i press that button.
<?php if (isset($_POST['finduser_btn']) && $noerr) :
echo "<div class='green'>
<button type='submit' class='btn' name='scoreDel'>Delete scores</button>
</div>
endif ?>
I use $noerr as FLAG to display or not the button if another button is pressed (the other button is not shown in code)
Well, how do i use scoreDel button to run a query like:
DELETE FROM scores
WHERE name = '$username$;
I think i have some issue with " and ' into the PHP echoing html tags but i'm not sure... I hope in some help, i'm getting mad.
Thanks in advance
You need a form in order to submit your action.
echo '<form action="mypage.php" method="POST"><div class="green">
<button type="submit" class="btn" name="scoreDel">Delete scores</button>
</div></form>';
Try the following:
<?php if (isset($_POST['finduser_btn']) && $noerr) : ?>
<div class='green'>
<form method="post">
<input type="text" name="finduser">
<button type='submit' class='btn' name='scoreDel'>Delete scores</button>
</form>
</div>
<?php endif ?>
Use a form to submit the button tag. Also, write html outside of PHP code if possible.

Passing value of textarea with jquery to PHP

I have a tip section that can be "custom" input from a customer. When the customer enters a value, I want to pass it to PHP for updating the order.
I'm unsuccessful and I'm thinking I'm approaching this problem the wrong way since I can't get it to work. What options are available to me without using AJAX?
The tip section:
<?= ($receipt['tip'] > 0 ?
'<tr id="tip-area"><th>Tip</th>
<td><textarea id="tip" name="update_tip" readonly>'. $receipt['tip'].'</textarea></td>
</tr>'
: '') ?>
My form which has different tip options:
<form method="post">
<tr>
<button title="20% Tip" type="submit" name="update_tip" id="update_tip" class="tip-button"
value="<?= ($_SESSION['order']['quote']['subtotal'] * 0.2); ?>">
<small>20%<br><?= number_format(($_SESSION['order']['quote']['subtotal'] * 0.2), 2) ?></small>
</button>
<button title="Edit Tip" type="button" name="update_tip" id="custom-tip" class="tip-button">
<small>Edit<br>Tip<br></small>
</button>
<button title="Save Tip" type="submit" id="save_tip" class="hidden">
<small>Save<br>Tip</small>
</button>
</tr>
</form>
My jQuery:
$('#custom-tip').click(function(){
$('#tip').removeAttr('readonly').focus();
$('.tip-button').addClass("hidden");
$('#save_tip').removeClass("hidden");
});
$('#save_tip').click(function (){
var tip = $('textarea#tip').val();
$('<input type="hidden" name="update_tip" value="' + tip + '">').submit();
});
$('#tip').focus(function(){
this.value = '';
});
When they press "Edit Tip", the readonly property is remove, the area comes into focus and value is cleared.
Then the user should enter a value and hit Save.
Then I'm trying to retrieve the value they entered.
I think this is what you want:
$("form").submit(function() {
$("<input>", {
type: "hidden",
name: "update_tip",
value: $("#tip").val()
}).appendTo($(this));
});
This will create the hidden input with the value from the textarea and append it to the current form when before the form is submitted.
You need to have PHP code on the same page (because your form does not specify an action) that handles the data. Use the 'name' attribute for your input to specify the key of the value you wish to access in the $_POST super global. For example, if I post a form with the following code:
<form method='POST'>
<input type='hidden' value='hello' name='world'>
<input type='submit' value='submit'>
</form>
Then in PHP, I can access the value of the element with the name "world" with the following code:
$_POST['world']
Use this syntax to acquire the data from the form and persist it/update it.

HTML + Button with text and image on it

I have a problem in creating a Button with text and image on it.
<td>
<button type="submit" name="report" value="Report"
<?php
if($tab == 'Excel')
echo "id=\"tab_inactive\"";
else
echo "id=\"tab_active\"" ; ?>>
<img src="images/report.gif" alt="Report"/>Report
</button>
<button type="submit" name="excel" value="Excel"
<?php
if($tab == 'Excel' )
echo "id=\"tab_active\"";
else
echo "id=\"tab_inactive\"" ; ?>>
<img src="images/Excel.gif" alt="Excel" width="16" height="16" /> Excel
</button>
</td>
Here $tab is
$tab = strlen(trim($_POST[excel]))>0 ? $_POST[excel] : $_POST[report];
I tried this way, but this is behaving so strangely.
On click the button:- The submit function is working properly in firefox, but not in IE.
Instead of submitting the value(in this example values are 'Report' and 'Excel'), indeed it is submitting the label of the button.
That is if i am checking the value of array PRINT_R($_POST). The value of it is
Array ( [report] =>(Icon that i used) Report
[excel] => (Icon that i used) Excel
[frm_analysis] => [to_analysis] => )
Here i have more than 1 button, then all the labels are submitted eventhough one of them is pressed. i dont how to capture which button is pressed.
I even tried changing button type=button and onclick="document.formname.submit()" Even this is resulting in the same.
Can you please help me to solve this.
<button>s are broken in IE. I think they corrected it in IE7 or 8. Is the page in standards mode? That may help in IE7 or 8.
If you need to support the broken version, you could try and work around it with JS, if you don't mind being dependent on it. Example:
<script type="text/javascript">
function buttonclicked(button) {
button.form.elements["buttonclicked"].value = this.value;
}
</script>
<input type="hidden" name="buttonclicked" value="">
<button type="submit" name="report" onclick="buttonclicked(this)">
<img src="images/report.gif" alt="Report"/>Report
</button>
<button type="submit" name="excel" onclick="buttonclicked(this)">
<img src="images/Excel.gif" alt="Excel" width="16" height="16" /> Excel
</button>
I don't think there is any other way other than avoiding <button> and using <input type=submit> if you need to distinguish between which on was clicked.
This, unfortunately, is IE's default behaviour when dealing with <button> elements. There is, to my knowledge, no real solution to this problem, although there are ofcourse numerous workarounds.
My suggestion would be as follows:
Create a radio button control called report_type (or whatever you choose), with two radio buttons: One for Excel and one for Report. Also, include a regular submit button.
With Javascript (if it's enabled), you can hide the radio buttons and submit button (display: none), and create (through JS) two buttons, labeled Report and Excel. On clicking one of these buttons, the proper radio button is checked, and the form is submitted.
This will allow you to create the form in it's current form, while providing a working fallback for users without javascript support.
And instead of the button's value, you can now look at the Radio's value to decide which form of reporting you want to use.
<button title="Search" onClick="javascript:executeFunction();">Search<br /><img src="images/search_icon.gif"/><br />More Text</button>
since your image is included within the <button> (<input>?) tag, it is likely being included in the value.
Why not use an img with either an onclick event or a href with a GET request?
Have not tested it, just one idea:
<td>
<button type="submit" name="active_page" value="Report"
<?php
if( isset( $_POST['active_page'] ) && $_POST['active_page'] == 'Excel' )
echo "id=\"tab_inactive\"";
else
echo "id=\"tab_active\"" ; ?>>
<img src="images/report.gif" alt="Report"/>Report
</button>
<button type="submit" name="active_page" value="Excel"
<?php
if( isset( $_POST['active_page'] ) && $_POST['active_page'] == 'Excel' )
echo "id=\"tab_active\"";
else
echo "id=\"tab_inactive\"" ; ?>>
<img src="images/Excel.gif" alt="Excel" width="16" height="16" /> Excel
</button>
</td>
Dont really understand your problem, is it this:
You want to now wich Button was clicked, to show the tab or and other tab? Right?

Categories