Send parameter to url in PHP - php

I'm having trouble sending a parameter into a url of another page. I'm new to coding in PHP so I do not really know how to get this through, already did some research on this about the $_GET method, but its still not working.
Code in 1st page:
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</a></td></tr>";
where $temp is the parameter I want to pass to the url.
Code in 2nd page:
$id = $_GET['id'];
$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = $id");
//$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = 10");
The sql query returns error that the $id is null, and the url doesn't display the id.

Do it like this on your html line
echo "<tr><td>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</td></tr>";

I ran your code to see what it output:
// Make some sample data
$row = [
'appl_nric_date' => '9999-99-99',
'applicant_name' => 'some-applicant',
'nric' => 'wtf-is-an-nric',
'application_date' => '8888-99-00'
];
$temp = 'something';
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>"
. $row['appl_nric_date']
. "</td><td>"
. $row['applicant_name']
. "</td><td>"
. $row['nric']
. "</td><td>"
. $row['application_date'] . "</a></td></tr>";
echo PHP_EOL;
This is what it outputs:
<tr><td><a href='application_desktop.php?id='something'>9999-99-99</td><td>some-applicant</td><td>wtf-is-an-nric</td><td>8888-99-00</a></td></tr>
The use of single quotes is not right. Remove the single quote after id=.
Looks like you are not nesting your html elements correctly. You place the opening A tag inside the first TD but then you close that TD without closing the A tag.
In order to debug what you are doing this in the browser, then the address bar where the url lives should show the parameters that are sent to the destination page. You can just look at that to verify that it sent what you intended.
In the destination page, you can add the following to debug:
<pre>
<?php print_r($_GET) ?>
</pre>
The above will let you see what you are getting from the first page.

Related

PHP not loading inline

I have a php file with the following code which is run inside a switch statement:
switch($valueFoo) {
case 'bar':
echo "<select id=\"selTheme\">";
$path = './files/css/themes/';
$files = array_values(array_diff(scandir($path), array('.', '..')));
for ($i = 0; $i < count($files); $i++) {
$cVal = substr($files[$i], 0, -4);
$cTitle = ucwords(substr($files[$i], 0, -4));
if ($cTitle==$_SESSION['setTheme']) {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\" selected>" . $cTitle . "</option>";
} elseif ($i>=count($files)) {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\">" . $cTitle . "</option></select>";
} else {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\">" . $cTitle . "</option>";
}
}
echo "...";
This is intended to create a list of options from a folder on my server. And it does indeed work. The problem is that it ONLY works AFTER the page is refreshed. I have been banging my head on my table trying to figure out why it does this only after the page is refreshed. I have no clue. I want the element to be filled with options as soon as it loads on page. I don't want the page to reload at all. It works by itself to populate an unordered list but I want it to be selectable options.
I don't see anything wrong with the code at all. I don't understand why the options list aren't being populated without a reloading of the page. I don't understand why it fills in perfectly when the page reloads. I would think that if it would do it properly AFTER the reload, it would do it just fine the first time it loads! Why isn't?
Please help me understand.
EDIT: This code comes as a return from an AJAX call. I am trying to run the for loop from that AJAX call. The loop doesn't run until the page reloads. Is there a way to force the AJAX call without the page load?
if you want a dynamic display of the folders content you'll have to use Ajax.
make sure that the session is already created and you're using 'session_start()' in the beginning of all your pages,
clarify your question/code to get precise answers

Empty parameter when passing through header location

I'm setting a variable based on an array. When I echo the variable it displays on the screen, however when I try to add it to a Header Location it doesn't show up in the URL of the next page - everything else does:
$myid = $selected_cat3[0]['id'];
Header("Location:/cat-dashboard/cat-results/?catID=" . $myid
. "&question1=" . $_GET['question1'] . "&question2=" . $_GET['question2']
. "&question3=" . $_GET['question3'] . "&question4=" . $_GET['question4']
. "&question5=" . $_GET['question5'] . "&question6=" . $_GET['question6']
. "&question7=" . $_GET['question7'] . "&question8=" . $_GET['question8']);
This is the generated url:
/cat-dashboard/cat-results/?catID=&question1=0&question2=3&q‌​uestion3=1&question4‌​=1&question5=1&quest‌​ion6=2&question7=1&q‌​uestion8=3
Am I doing something wrong? It doesn't show even if I use: $myid = "1";

php in header url error

I have just a quick question. i was using a normal html link tag to redirect to a paypal checkout page and it was working fine even when i had php inside the url. but when i was using it in a php header
the url cuts off where i enter the php.
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product . " " . $server ?>&amount=<? echo $xprice1; ?>%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
You are placing the PHP code inside of the location redirect as a string. The code is not being evaluated as PHP.
Try this instead:
<?php
$url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest";
header('location: ' . $url);
Or, you could keep it in one line like so:
<?php
header('location: ' . "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest");
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product' . " " . '$server ?>&amount=<? echo $xprice1; ?>%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
This may solve your problem. Please dont forget to tick the answer right if it works.

How would you do a code-escape in PHP?

I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an XML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}
I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.
Check out htmlspecialchars and htmlentities
use CDATA construction:
echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";
If this is your entire script, fastest way would probably be to swap all of the <'s with <
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}

How do I update an html table after each XML call to the server without refreshing the page?

I have a mySQL table loaded with 50 rows. Each row has the necessary information to process a credit card. When the user clicks on Process Credit Cards, query the table and display each row on the page using html. Once the data has been displayed on the page a scrip would begin to process each row through the merchant account and turn the corresponding row either red for decline or green for approve without refreshing the page after each transaction. I think I need to use AJAX or jQuery to make this happen but I'm not sure I'm headed in the right direction. Here is the script to process the transactions:
<?php
$request = new GatewayRequest();
$response = new GatewayResponse();
$service = new GatewayService();
$request->Set(GatewayRequest::MERCHANT_ID(), "111111111111111");
$request->Set(GatewayRequest::MERCHANT_PASSWORD(), "xxxxxxxxxxxx");
$time = time();
$request->Set(GatewayRequest::MERCHANT_CUSTOMER_ID(), $time . '.PHPTest');
$request->Set(GatewayRequest::MERCHANT_INVOICE_ID(), $time . '.SaleTest');
$request->Set(GatewayRequest::AMOUNT(), "9.99");
$request->Set(GatewayRequest::CARDNO(), "4111111111111111");
$request->Set(GatewayRequest::EXPIRE_MONTH(), "02");
$request->Set(GatewayRequest::EXPIRE_YEAR(), "2010");
$request->Set(GatewayRequest::CVV2(), "999");
$request->Set(GatewayRequest::CUSTOMER_FIRSTNAME(), "Joe");
$request->Set(GatewayRequest::CUSTOMER_LASTNAME(), "PHPTester");
$request->Set(GatewayRequest::EMAIL(), "phptest#fakedomain.com");
$request->Set(GatewayRequest::IPADDRESS(), $_SERVER['REMOTE_ADDR']);
$request->Set(GatewayRequest::BILLING_ADDRESS(), "123 Main St");
$request->Set(GatewayRequest::BILLING_CITY(), "Las Vegas");
$request->Set(GatewayRequest::BILLING_STATE(), "NV");
$request->Set(GatewayRequest::BILLING_ZIPCODE(), "89141");
$request->Set(GatewayRequest::BILLING_COUNTRY(), "US");
$request->Set(GatewayRequest::SCRUB(), "IGNORE");
$request->Set(GatewayRequest::CVV2_CHECK(), "IGNORE");
$request->Set(GatewayRequest::AVS_CHECK(), "IGNORE");
$service->SetTestMode(TRUE);
if ($service->PerformPurchase($request, $response)) {
print "Purchase succeeded\n";
print "Response Code: " .
$response->Get(GatewayResponse::RESPONSE_CODE()) . "\n";
print "Reasone Code: " .
$response->Get(GatewayResponse::REASON_CODE()) . "\n";
print "Auth No: " . $response->Get(GatewayResponse::AUTH_NO()) . "\n";
print "AVS: " . $response->Get(GatewayResponse::AVS_RESPONSE()) . "\n";
print "CVV2: " . $response->Get(GatewayResponse::CVV2_CODE()) . "\n";
print "GUID: " . $response->Get(GatewayResponse::TRANSACT_ID()) . "\n";
print "Account: " .
$response->Get(GatewayResponse::MERCHANT_ACCOUNT()) . "\n";
print "Scrub: " .
$response->Get(GatewayResponse::SCRUB_RESULTS()) . "\n";
} else {
print "Purchase failed\n";
print "GUID: " . $response->Get(GatewayResponse::TRANSACT_ID()) . "\n";
print "Response Code: " .
$response->Get(GatewayResponse::RESPONSE_CODE()) . "\n";
print "Reasone Code: " .
$response->Get(GatewayResponse::REASON_CODE()) . "\n";
print "Exception: " .
$response->Get(GatewayResponse::EXCEPTION()) . "\n";
print "Scrub: " .
$response->Get(GatewayResponse::SCRUB_RESULTS()) . "\n";
}
?>
Will this type of code work with AJAX or jQuery without being rewritten? Any help would be appreciated.
Anything can be made to work without being rewritten, but you've set yourself up for a lot of headaches there. You'll probably have much better luck (and save yourself tons of time) by formatting all those print statements into an array and JSON encoding it. Obviously javascript loves JSON.

Categories