collapse

* User Info

 
 
Welcome, Guest. Please login or register.
Did you miss your activation email?

* Who's Online

  • Dot Guests: 64
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Shoutbox

Refresh History
  • Shoutbox is not for support!
  • BurkeKnight: [SiNaN] old friend. Great Job. I can't wait to see what you add next. :)
    October 13, 2014, 04:13:53 PM
  • [SiNaN]: Thank you everyone, for the kind words and support. For those who are interested, check out our Donators Program: [link]
    October 13, 2014, 11:57:44 AM
  • {OCS}MasterSeal: Glad to be here, glad to use your amazing portal THank you!
    October 13, 2014, 11:33:35 AM
  • Mike Vail: I'd like to donate to you for all your wonderful work. Do you guys have a place on your site to do that? THANKS FOR EVERYTHING!
    October 13, 2014, 11:12:16 AM
  • Mstcool: Yay, an update! :D
    October 12, 2014, 01:39:17 AM
  • stanyy: You're right. I have no idea how I got to the "Archived" index. It looks too similar.
    October 03, 2014, 11:38:32 AM
  • [SiNaN]: SimplePortal 2.3.5 package is listed in Stable SimplePortal Packages category in Downloads area.
    October 03, 2014, 06:08:01 AM
  • stanyy: SP 2.3.5 is not available in the downloads section?? The big download link on the homepage points newcomers to the downloads section who then download 2.3.4. Not good.
    October 03, 2014, 03:54:21 AM
  • Mulwa: if simpleportal team could make some of the blocks like articles to display photos just like how ebay does, it would be very great for shopping
    September 27, 2014, 08:24:14 AM
  • Mulwa: everybody is busy, no help for us this time
    September 26, 2014, 05:00:07 PM
  • [SiNaN]: That shows how simple but powerful SimplePortal is!
    September 26, 2014, 06:56:41 AM
  • oNLy1: thought the 'Blocks In Blocks' was going to be complicated - easy peasy - and was exactly what my site needed - ty
    September 20, 2014, 01:13:44 PM
  • P flex: Xup guys
    September 18, 2014, 08:29:06 AM
  • P flex: Xup
    September 18, 2014, 08:28:38 AM
  • Mstcool: :p *
    September 13, 2014, 07:55:49 PM
  • Mstcool: so when is a new version coming out? :o
    September 13, 2014, 07:55:45 PM
  • USFA: sorry thought was search no glasses
    September 06, 2014, 08:48:23 PM
  • USFA: rename forum
    September 06, 2014, 08:47:40 PM
  • Mstcool: Oh, ok, Sinan
    August 25, 2014, 10:19:15 PM
  • Arsen: When i make them
    August 19, 2014, 12:20:20 AM

* Recent Posts

Re: Facebook style wall posting integrated with Simple Portal and user profile by rexall
[Today at 07:15:44 AM]


Re: Shoutbox refresh and page reload discrepancy by i8088
[October 23, 2014, 04:44:24 PM]


Staff list - Sort by rank by ThatCrazyGuy
[October 22, 2014, 04:16:44 PM]


Re: SimplePortal 2.3.6 Released! by andy
[October 22, 2014, 08:35:00 AM]


Re: Blok İçinde Bloklar Oluşturmak by mkuru
[October 22, 2014, 04:27:32 AM]

NEED HELP? If you're looking for support with Simple Portal, look no further than the Support Board!

Author Topic: PHP Page submitting POST to itself  (Read 5272 times)

0 Members and 1 Guest are viewing this topic.

Offline mrcopper

  • Newbie
  • Posts: 4
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
PHP Page submitting POST to itself
« on: January 31, 2013, 06:33:11 PM »
Hello,
        I'm looking to create a PHP page through the simpleportal interface that will accept data into a form and then POST to itself, use the data to call a fucntion included through @require_once. I've done a bit of development already but am still a bit new.

This is what I have for the page code so far:
Code: [Select]
//@require_once($boarddir .  '/Webhooks/countryassoc.php');
if(isset($_POST['submit']))
{
    $countrynum = $_POST['num'];
    assoc_country($countrynum);
}
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
         Country Number :<input type="text" name="num"><br>
         <input type="submit" name="submit" value="Change Country Number"><br>
         </form>';
The @require is commented because I'm still fighting with it right now. Here is the content of countryassoc.php
Code: [Select]
<?php
function assoc_country($countrynum)
{
global 
$user_info;
$query $smcFunc['db_insert']('','earth.usertocountry',
array(
'id_member' => 'int''countrynum' => 'int'),
array(
$user_info['id'], $countrynum),
array(
'id_member','countrynum')
);
echo 
"Country Number Changed!";
return 
0;
}


?>



Thanks in advance for any information you can offer

Offline Underdog

  • Support
  • *
  • Posts: 814
  • Gender: Male
  • On Hiatus
    • WebDev
  • SMF Version: 2.0.9
  • SP Version: 2.3.6
Re: PHP Page submitting POST to itself
« Reply #1 on: February 01, 2013, 07:59:16 AM »
mrcopper,

  At the onset of your block, prior to your require command you neglected to call the $boarddir global.

Code: [Select]
global $boarddir;
@require_once($boarddir .  '/Webhooks/countryassoc.php');
if(!empty($_REQUEST['num']) && isset($_REQUEST['save']))
{
    $countrynum = $_REQUEST['num'];
    assoc_country($countrynum);
}
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
         Country Number :<input type="text" name="num" /><br />
         <input type="submit" name="submit" value="Change Country Number" /><br />
         </form>';


Offline mrcopper

  • Newbie
  • Posts: 4
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
Re: PHP Page submitting POST to itself
« Reply #2 on: February 01, 2013, 08:17:22 AM »
Underdog,
     Thanks, that seems to have cleared up part of the issue. However, after the submit I am still having problems which I believe reside in the included php file
Code: [Select]
<?php
function assoc_country($countrynum)
{
global 
$user_info;
$smcFunc['db_insert']('','earth.usertocountry',
array(
'id_member' => 'int''countrynum' => 'int'),
array(
$user_info['id'], $countrynum),
array(
'id_member')
); 
echo 
"Country Number Changed!";
}


?>


This is my first time attempting to use smfFunc['db_insert'] so I'm not sure if I'm using it correctly.

Offline Underdog

  • Support
  • *
  • Posts: 814
  • Gender: Male
  • On Hiatus
    • WebDev
  • SMF Version: 2.0.9
  • SP Version: 2.3.6
Re: PHP Page submitting POST to itself
« Reply #3 on: February 01, 2013, 12:58:18 PM »
mrcopper,

  You are not informing us of the error you are receiving from it but I will hazard a guess to say the table does not exist as you did not initially create it and also you forgot the $smcFunc global. Also you can set the necessary variable(s) within the brackets which will only take affect if they were initially undefined.

For this setup, I suggest not using a period in your table name.. try underscore. Also you need a key column in your table set to auto increment (set this up when creating your table).

Below I put some logic to also ensure the value of $countrynum is not below the integer of 1 where I assume you are not using that value for a country id #... change the logic to less than 0 if you are.

There is an extra query below to check if the table exists.. once it is created you could remove the code but I wouldn't bother.. just leave it.

Code: [Select]
<?php
if (!defined('SMF'))
die('Hacking attempt...');

/* Main function */
function assoc_country($countrynum 0)
{
global $user_info$smcFunc;

if ((int)$countrynum || (int)$user_info['id'] < 1)
return false;

if (!check_table_exists_mycountry('earth_usertocountry'false))
{
$smcFunc['db_query'](''"CREATE TABLE {db_prefix}earth_usertocountry
(
`reference` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_member` mediumint(8) unsigned NOT NULL default 0,
`countrynum` int(10) unsigned NOT NULL default 0,
PRIMARY KEY (`reference`))"
);
}

$smcFunc['db_query'](''"INSERT INTO {db_prefix}earth_usertocountry (`id_member`, `countrynum`) VALUES ({int:member}, {int:country})", array(member => (int)$user_info['id'], country => (int)$countrynum));

echo "Country Number Changed!";
}

/* Check if table exists */
function check_table_exists_mycountry($table$checkval false)
{
global $smcFunc;
$check $smcFunc['db_query'](''"SHOW TABLES LIKE {db_prefix}{string:mytable}", array(mytable => $table));
$checkval $smcFunc['db_num_rows']($check);
$smcFunc['db_free_result']($check);
if ((int)$checkval 0)
return true;

return false;
}
?>

 
« Last Edit: February 01, 2013, 01:53:41 PM by Underdog »

Offline mrcopper

  • Newbie
  • Posts: 4
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
Re: PHP Page submitting POST to itself
« Reply #4 on: February 02, 2013, 10:28:09 PM »
So I have attempted to implement some of the advice you gave me. I was using earth.usertocountry because I was storing stuff for my changes to another database on the same server. It worked properly for a select statement so I figure it shouldn't be an issue for insert/update.

Also, the table was infact created but I was not using an auto-incrementing primary key. I recreated the table to look like the one in your version of the script. Here is what I have now below. Still getting problems. I'm not getting an error, after I submit the number to be changed I'm getting a blank page and nothing is inserted into the database, no error though.

Code: [Select]
<?php
if (!defined('SMF'))
die('Hacking attempt...');
function 
assoc_country($countrynum 0)
{
global 
$user_info;
global 
$smcFunc;
echo 
"got this far";
if ((int)
$countrynum || (int)$user_info['id'] < 1)
return false;

$query $smcFunc['db_query']('',"SELECT * FROM earth.usertocountry WHERE id_member={int:user_id}",array('user_id' => $user_info['id']));
// Check to see if the user already has a country number assigned
if ($smcFunc['db_fetch_assoc']($query) == || $smcFunc['db_fetch_assoc']($query) == nil) {
$query2 $smcFunc['db_query']('',"INSERT INTO earth.usertocountry (`id_member`, `countrynum`) VALUES ({int:member}, {int:country})",array('member' => (int)$user_info['id'], 'country' => (int)$countrynum));
} else {
$query2 $smcFunc['db_query']('',"UPDATE earth.usertocountry SET 'countrynum'={int:country} WHERE 'id_member'={int:member}", array('country' => (int)$countrynum 'member' => (int)$user_info['id']));
}

}


?>

Offline mrcopper

  • Newbie
  • Posts: 4
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
Re: PHP Page submitting POST to itself
« Reply #5 on: February 04, 2013, 10:07:49 PM »
So I've left this problem behind and moved on to others. The main error I always seem to get is this blank page. Is there anyway to get a more detailed error than a blank page out of this?

Offline Underdog

  • Support
  • *
  • Posts: 814
  • Gender: Male
  • On Hiatus
    • WebDev
  • SMF Version: 2.0.9
  • SP Version: 2.3.6
Re: PHP Page submitting POST to itself
« Reply #6 on: February 05, 2013, 04:05:30 PM »
mrcopper,

  Atm I do not know why but the arrays were not working within $smcFunc.. probably something I'm not noticing that I put in err.

  Anyhow, I rewrote & tested the code below which functions as you wish:

php file (folder & file are lower case) ~ file is attached:
webhooks/countryassoc.php
Code: [Select]
<?php
if (!defined('SMF'))
die('Hacking attempt...');

/* Main function */
function assoc_country($countrynum 0)
{
global $user_info$smcFunc;

if ((int)$countrynum || (int)$user_info['id'] < 1)
return false;

$memberid = (int)$user_info['id'];

if (!check_table_exists_mycountry('earth_usertocountry'false))
{
$smcFunc['db_query'](''"CREATE TABLE {db_prefix}earth_usertocountry
(
`reference` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_member` mediumint(8) unsigned NOT NULL default 0,
`countrynum` int(10) unsigned NOT NULL default 0,
PRIMARY KEY (`reference`))"
);
}

if (!check_user())
{
$smcFunc['db_query'](''"INSERT INTO {db_prefix}earth_usertocountry (`id_member`, `countrynum`) VALUES ({$memberid}{$countrynum})");
$message "Country Number Added!";
}
else
{
$smcFunc['db_query'](''"UPDATE {db_prefix}earth_usertocountry SET countrynum = {$countrynum} WHERE id_member = {$memberid}");
$message "Country Number Changed!";
}

return $message;
}

/* Check if table exists */
function check_table_exists_mycountry($table$checkval false)
{
global $db_prefix$smcFunc;
$check $smcFunc['db_query'](''"SHOW TABLES LIKE '{$db_prefix}$table'");
$checkval $smcFunc['db_num_rows']($check);
$smcFunc['db_free_result']($check);
if ((int)$checkval 0)
return true;

return false;
}

function 
check_user()
{
global $smcFunc$user_info;

if ((int)$user_info['id'] < 1)
return false;

$userid = (int)$user_info['id'];

$result $smcFunc['db_query'](''"SELECT id_member FROM {db_prefix}earth_usertocountry WHERE id_member = {$userid} LIMIT 1");
$check $smcFunc['db_num_rows']($result);
$smcFunc['db_free_result']($result);
if ((int)$check 0
return true;

return false;
}


function 
currentPage($pageURL 'http://'
{
        
if (!empty($_SERVER["HTTPS"]))
$pageURL "https://";

if ((!empty($_SERVER["SERVER_PORT"])) && $_SERVER["SERVER_PORT"] != "80"
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else 
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 

return $pageURL;
}  
?>


Custom php block:
Code: [Select]
global $boarddir, $context;
@require_once($boarddir .  '/webhooks/countryassoc.php');
if(!empty($_REQUEST['num']) && isset($_REQUEST['submit']))
{
    $countrynum = (int)$_REQUEST['num'];
    $context['countryNumber'] = assoc_country($countrynum);     
}

$message = !empty($context['countryNumber']) ? $context['countryNumber'] : false;

echo '
<form action="' . currentPage() . '" method="post">
<span style="text-align:left;">Country Number :<input type="text" name="num" maxlength="4" style="width:40px;" /></span><br />
<span style="text-align:left;"><input type="submit" name="submit" value="Change Country Number" /></span><br />
</form>
<span style="color:red;text-align:left;">', $message, '</span>';

  I put some span tags with style attributes just so you get the concept of having it display as you wish.
Look up some css references at w3schools or similar to adjust those to your liking.
text-align:left; is actually default & is not necessary but I put it in as an example.

  This will add a new table if it does not exist, add a new entry if one does not exist for the user or update/edit an existing entry for the user id.

  Also put the attached index.php file in your webhooks folder & please notice that the folder & file names are lower case in this example. The index file will help protect the directory by not allowing access to view its contents.

 
« Last Edit: February 05, 2013, 07:35:00 PM by Underdog »