Simple PHP button to send to next page - php

I have this:
echo "<form method='post' action='{$_SERVER['PHP_SELF']}?pagenum=$pagenum'>";
echo "<input type='hidden' value='";
echo htmlspecialchars($pesquisa);
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></form>";
But I need only a button that when clicked for the first time, sends to page two. When it's clicked again on that page two, send do page three and so on. How do I do this in a simple way?

You should just use a link instead of a form. if it must look like a button, you can use an image.
echo "<img src=\"button.jpg\" />";

Assuming $pagenum starts at 0.
if($_POST['pesquisa'])
{
$pagenum++;
}
else
{
$pagenum = 0;
}
That way your first page view you wont have a $_POST['pagenum'] so it sets to 0. After that you will have one as your passing it along and then you increment the value for the next button press.

Here the button:
$pesquisa = 'label';
$pagenum = 100500;
echo "<input type='button' onclick='location.href=\"{$_SERVER['PHP_SELF']}?pagenum={$pagenum}\"'";
echo " value='" . htmlspecialchars($pesquisa) . "'/>";
All logic to change current page to next one showed in previous answers.

Related

Php - echoing an html textbox, setting the textbox's value to a variable: returns only up to the first space of the string

So within my php code: I'm trying to auto generate and populate a form that will edit a database.
$aName = "Wayne Gretzky";
echo "<form>";
echo "<input type='text' value=".$aName.">";
echo "<input type='Submit'>";
echo "</form>";
But when the code executes it shows a textbox with the value of "Wayne" rather than "Wayne Gretzky"
Because i want the form to be auto generated, so it can adapt to whatever data I may pull form the database; How do i call the variable or the result of a query so that the full string will be displayed in the textbox?
You need to put quotes on the value attribute as well, otherwise the browser does it for you and then you get what you got.
echo "<input type='text' value='" . $aName . "'>";
You are going wrong with the quotes.
If you are using double quotes outside, use single quotes inside for the html tags.
The correct code is
<?php
$aName = "Wayne Gretzky";
echo "<form>";
echo "<input type='text' value='$aName'>";
echo "<input type='Submit'>";
echo "</form>";
?>

Trying to pass variable in url

I'm echoing this form:
echo "<form action='/leaguemaster/fichaTorneio.php?id=" . $_GET['torneioid'] . "'>";
When I submit the form and it links to the page from the action, the variable I'm passing doesn't reach the page...
/leaguemaster/fichaTorneio.php?
I've done this in other pages and it worked, I don't know what's going on here. I've tried having the same form outside a PHP echo and I get the same result.
-EDIT-
This is the full form:
echo "<form method='GET' action='/leaguemaster/fichaTorneio.php?id= " . $_GET['torneioid'] . "'>";
echo "<input type='submit' value='Voltar'>";
echo "</form>";
Use hidden type form's element named id:
echo "<form action=\"/leaguemaster/fichaTorneio.php\">";
echo "<input type=\"hidden\" name=\"id\" value=\"$_GET[torneioid]\" />"
Notice: I did not set the method attribute to the form because the GET method is the default method for HTML forms and it makes the form submits its values through the URL query string.
Please try
// check $_GET['torneioid'] value
echo 'torneioid = ' . $_GET['torneioid'] . '<br />';
echo '<form action="/leaguemaster/fichaTorneio.php?id=' . $_GET['torneioid'] . '">';
And make sure the URL contains "http:// yourdomain.com/?torneioid=something"
There are several things that you should check:
$_GET['torneioid']
This indicates that you are attempting to resolve form or other variables through GET. Make sure that you are passing information through GET and not POST.
Also make sure there is something that is actually stored in torneioid, and that the form or database that you are 'GETting' from, actually holds a value.

PHP: using an html Form to call up a PHP action

I am trying to create a 'submit' input type button that when clicked will call up a switch case action that I've defined in PHP. When the 'submit' button is clicked, I want the form action to essentially create a link and display in the URL form action so that it is called properly by the PHP file.
However when I click on the submit button, the URL does not properly display the desired link and action.
PHP:
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
echo "<form action='enter_time.php?uid='" . $userid . "?action=timesubmit method='get'>";
echo "<td><input name=submit_time type=submit id=submit_time" . $time_cell_row . $time_cell_column . "></input></td>";
echo "</form></tr>";
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
The problem now is when I click on the 'submit' button, the URL displayed enter_time.php?submit_time=Submit" instead of "enter_time.php?uid=3?action=timesubmit
You might want to add in the final apostrophe after timesubmit
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='post'>";
You have a quote after uid that should not be there:
"<form action='enter_time.php?uid=" . $userid . "?action=timesubmit method='get'>";
If you are using a form to submit GET variables into a url you could do something like
<a id="submit_time<?= $time_cell_row . $time_cell_column ?>" href="enter_time.php?uid=<?= $userid ?>">Submit Time</a>
If you prefer to use a form, writing it this way looks clearer to me
PHP
<?php
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
?>
<form action='enter_time.php' method='get'>
<input type="hidden" name="action" value="<?= $timesubmit ?>">
<input type="hidden" name="uid" value="<?= userid ?>">
<input name="submit_time" type="submit" id="submit_time<?= $time_cell_row . $time_cell_column ?>" />
</form>
<?php
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
?>
Just off top of my head it should be:
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='get'>";
Single quote after "uid=" was in the wrong place. Shouldn't be until after "timesubmit".

Unsure how to get $_POST[''] value from form when the name is dynamic?

So at the moment I have the following code which is correct but I'm just unsure how I will be able to get the name=".$job_id." as it will be dynamic, applicant-jobs.php below:
<div id="container">
<?php
foreach($jobs as $job){
$job_id = $job['id'];
echo form_open('applications/applicants');
echo "<div class=\"job\">";
echo $job['name'];
echo "</div>";
echo "<input type=\"hidden\" name=".$job_id.">";
echo form_submit('submit', 'View Applicants');
echo form_close();
}
?>
Any help is greatly appreciated, many thanks. P.S. I'm using codeigniter.
You can iterate arrays by key value pairs.
foreach($_POST as $key => $val)
{
}
Read more on the docs here; http://php.net/manual/en/control-structures.foreach.php
Edit, I slightly misunderstood your question. What you're looking for is the value attribute of the <input> element:
echo "<input type=\"hidden\" name=\"job\" value=\"".$job_id."\">";
Then in PHP you just access it like:
$job_id = $_POST['job'];
By the looks of it, the name doesn't need to be dynamic at all. You're only using one per form.
The name can be static, and have the value dynamic.
ex:
echo "<input type=\"hidden\" name=\"job_id\" value=\"".$job_id."\" />";
it can be accessed via $_POST['job_id']
Don't you want to set the value rather than the name?
eg.
echo "<input type=\"hidden\" name=\"job_id\" value =\"".$job_id."\">";
Also you've got the from open and close within the loop, i think you want them outside the loop otherwise you have multiple forms and will only end up submitting one.

PHP Refresh button onclick

I have this while loop that echoes entries from a SQL database. I have an "Add to Cart" button which is supposed to remove the entry from the list and add it somewhere else. This part works, but once I click on the "Add to Cart" button, the entry is not removed right away, I have to manually refresh the page or visit another page and come back in order to see that the item is removed. I tried using the following (and few similar) lines on the onclick propery of the button, but it didn't work.
echo "<br /><center><input type='submit' name='submitAdd' value='Add to Cart' onclick='window.location.reload();'></center>";
Any suggestions are appreciated.
Thank you.
EDIT: This is the while loop that I use to generate the table. It is within a tag which is within the .
while ($row = mysql_fetch_array($song_query)) {
foreach ($_SESSION['selected_items'] as $key => $value) {
if ($value == $row['Item_ID']) {
$rowID = $row['Item_ID'];
echo "<tr style='background-color: #66FFFF;'>";
echo "<td><input type='checkbox' name='removeFromCart[]' value='$rowID'></td>";
echo "<td>" . $row['Item_ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Artist'] . "</td>";
echo "<td>" . $row['Album'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Year'] . "</td>";
echo "<td>" . $row['Bit_Rate'] . "</td>";
echo "<td>" . $row['Sample_Rate'] . "</td>";
echo "</tr>";
}
}
}
PHP is a server-side language, so any "actions" will be executed on the server, before the results are displayed to the user. In order to modify the page after it has been loaded on the user's browser, you have to use a client-side language, such as JavaScript. If you want to show an update to your cart without having to reload the page, then using an AJAX request is your best option. Check out the tutorial at http://www.w3schools.com/ajax/ and let me know if you have any questions about how to implement one :)
Update: Force Reload
If you want to just stick with reloading the page as is, try onclick='window.location.reload(true);' instead of onclick='window.location.reload();'. The true parameter forces a get request to the server. http://www.w3schools.com/jsref/met_loc_reload.asp
I use a link that references to the same page
Recargar
If the only feature you are looking for is to reload the page on the button's click, the way I typically do that is onclick="history.go(0)"
But if the form button is what's submitting a form and already reloads the page then I don't see why you couldn't put the while loop after your other code like this,
CHECK FOR FORM() {
UPDATE SQL DB
}
WHILE () {
OUTPUT DB DATA
}
not a button but a simple link, but maybe still useful for some:
<?php
$page = $_SERVER['PHP_SELF'];
print "Reload this page";
?>
Use header() function to refresh a web page in PHP. The PHP header() function sends an HTTP header to a client or browser in raw form.
Before HTML, XML, JSON or other output has been sent to a browser or client, a raw data is sent with the request (especially HTTP Request) made by the server as header information.
Syntax:
header ("header:url of the page")
I hope that this can place you on the right way.

Categories