简体   繁体   中英

How can I post and get a php variable

I used the following php code to get the file extension of an uploaded file:

$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

But now I want to post this variable to an other php file which saves all the data in a database.

if (isset($_POST["submit"])) {
  $insertSQL = sprintf("INSERT INTO sounds (id, title, artist, category, subcategory, keywords, upload, format, size, download, rating, ip, date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['artist'], "text"),
                       GetSQLValueString($_POST['categoryID'], "text"),
                       GetSQLValueString($_POST['subcategoryID'], "text"),
                       GetSQLValueString($_POST['keywords'], "text"),
                       GetSQLValueString($_FILES['upload']['name'], "text"),
                       GetSQLValueString($_FILES['upload']['name'], "text"),
                       GetSQLValueString($_FILES['upload']['size'], "text"),
                       GetSQLValueString($_POST['download'], "text"),
                       GetSQLValueString($_POST['rating'], "text"),
                       GetSQLValueString($_POST['ip'], "text"),
                       GetSQLValueString($_POST['date'], "text"));

I know it is probably a stupid question. But how can I post and get this variable?

$filename = $_FILES['upload']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

//to insert value into DataBase

GetSQLValueString($ext, "text")

Why would you want to do that? You don't need to do a POST to save to a database. Just rewrite the other PHP code to be a function and call that function instead.

You should only do a POST if you wish to communicate between two machines (such as between a visitor of your website to your server or your server to another server).

Have you considered using $_GET ? It's a lot easier. Be careful of using $_REQUEST however (I would say it's best practice not to use it).

If you want to POST you could consider using cURL. The PHP Manual has plenty of examples so I won't put any extra ones here.

Alternatively it might even be easier to use jQuery to AJAX post. With just three lines of code you can POST your variables and get a response. Check out the [http://api.jquery.com/jQuery.post/](jQuery manual) which also has examples. The syntax you would probably use a starting point is:

$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
   alert("Data Loaded: " + data);
 });

Personally I would try the AJAX post first...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM