PHP save form data to file - php

I am trying to save form data to a file, this is what I have so far:
if(isset($_POST['connect'])) {
$host = "$dbuser=" . $_POST["host"];
$root = $_POST["root"];
$pass = $_POST["pass"];
}
I'm trying to write the form data to a file with the variable $dbhost="Formdata";
and I get this error:
Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\dev\admin2.0\install\index.php on line 55`

$host = '$dbhost="' . $_POST["host"] . '"';
echo $host;
Try this. You don't need to echo unless you are attempting to print to the page.

I'm not really sure what you trying to accomplish, but if you want to write some data to a file (for example), you could try something like this:
<?php
if (isset($_POST['connect'])) {
file_put_contents("file.txt", "$dbuser={$_POST["host"]}");
}
Also keep in mind that notation:
"$dbuser="
will expand variable in place (1), if a variable $dbuser exists, because you are using double quotes.
With single quotes you will need string concatenation operator . like this:
<?php
if (isset($_POST['connect'])) {
file_put_contents("file.txt", '$dbuser=' . $_POST["host"]);
}
But, if this all about plain debug, maybe print_r($_POST); will be sufficient?
Hope this helps!
PHP Reference: Strings (please read section "Variable parsing").

Related

PHP include dynamically generated filename?

while($x<=$num_of_cpkgs){
$cpkg_navtray = '\'' . $cpkg_array[$x] . '.html\'';
include $cpkg_navtray;
$x++;
}
I'm getting an error when I try this ... it works when I manually include the same value however ... for example, if $cpkg_navtray = 'test.html', I'm getting an error; however, when I directly include 'test.html' like include 'test.html';, it works. Why?
You dont need quotes in filename variable -
$cpkg_navtray = $cpkg_array[$x] . '.html';
Looking same Question but tag and desire different: Jut do these, just a simple concatenate.
while($x<=$num_of_cpkgs){
$cpkg_navtray = $cpkg_array[$x].'.html';
include $cpkg_navtray;
$x++;
}

PHP include statement containing echoed PHP variable

I have the required ID variable from this, (there are 1-6 possible values):
$new_product['ID'] =$row[2];
What I need is to echo a separate 'php-include' depending on this variable, so something like:
<?php include 'includes/size/prod/echo $row[2].php'; ?>
which would display, includes/size/prod/1.php, includes/size/prod/2.php etc
I don't understand how to phrase the 'echo' within the php.
There are a few ways:
//Concatenate array value in double quotes:
<?php include "includes/size/prod/{$row[2]}.php"; ?>
//Concatenate array value outside of quotes:
<?php include "includes/size/prod/".$row[2].".php"; ?>
//or, using single quotes:
<?php include 'includes/size/prod/'.$row[2].'.php'; ?>
//Concatenate variable (not array value) in double quotes:
<?php $page = $row[2]; include "includes/size/prod/$page.php"; ?>
See:
http://php.net/manual/en/language.operators.string.php
PHP - concatenate or directly insert variables in string
PHP include with a variable in the path
It's very dangerous to include your PHP files with this technic !!
You must prevent this by doing at least a control of included files are PHP
Now to respond to your question:
<?php
// ? $row2 contains more than 1 php file to include ?
// they are seperated by comma ?
$phpToInclude = NULL;
define(TEMPLATE_INCLUDE, 'includes/size/prod/');
if (isset($row[2])) {
$phpToInclude = explode($row[2], ',');
}
if (!is_null($phpToInclude)) {
foreach($phpToInclude as $f) {
$include = sprintf(TEMPLATE_INCLUDE . '%s', $f);
if (is_file($include)) {
// add validator here !!
include ($include);
}
else {
// file not exist, log your error
}
}
}
?>
dear use following working code
$row[2] = '';
$file_name = !empty($row[2])?$row[2]:'default';
$include_file = "includes/size/prod/".$file_name.".php";
include($include_file);
Things get evaluated in double quotes but not in single:
$var = "rajveer gangwar";
echo '$var is musician'; // $s is musician.
echo "$var is musician."; //rajveer gangwar is musician.
So better to use double quotes
Example:
// Get your dynamic file name in a single variable.
$file_name = !empty($row[0]) ? $row[0] : "default_file";
$include_file = "includes/size/prod/$file_name.php";
include($include_file);
You can use the dots for separating a string:
So for instance:
$path = 'includes/size/prod/'.$row[2].'.php';
include '$path';
Or you can put it in a variable:
$path = $row[2];
include 'includes/size/prod/$path.php';
Php is able to evaluate a variable within a string.

Assigning php POST array to Javascript array

I know this may sound similar to some past Q/As, I think mine is slightly different though.. I have a webpage which I want to dynamically load text file information. I upload the text file through an iframe and I want to save this information from php to Javascript. Whenever I try to save this as a regular variable it doesn't work so I have tried to do this by saving this information as a part of the $_POST array under a hidden form named $_POST['hidden_form']. Whenever I try to read the php into Javascript, I keep getting an error "Unexpected token ILLEGAL." I have tried the following two codes:
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . $_POST['hidden_form'][$i]. "';\n";
}
and
saved_form = <?php echo json_encode($_POST['hidden_form']); ?>;
Assigning a php array into a javascript array
I think the error has to do with the " ' " needed to specify the array but not sure. I have no idea where to go from here so any help would be GREATLY appreciated. If there are better methods to do this please let me know. Thanks in advance!
saved_form = '<?php echo addslashes(json_encode($_POST['hidden_form'])); ?>';
Or
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . addslashes($_POST['hidden_form'][$i]) . "';\n";
}
Both should work, probably had quotes breaking something?
the best way i have used is,
text/javascript
var saved_form = <?php echo json_encode($_POST['hidden_form']) ?>
Please note there are no Quotes around the php so your saved_form is an Object not a string json string witch would require you to to use var form_object = eval(saved_form)
#Lee might have meant this?
Just a note though i would not use the Raw $_POST pass it to a function that can loop though and addSlashes every value inside the post some thing like
<?php
function arr_addSlashes($array){
$ret = array();
foreach($array as $k => $v){
$ret[$k] = addSlashes($v);
}
return $ret;
}
?>

Get code to $_POST variable php

$newRequesterPass=mysql_real_escape_string($_POST['userPass']);
$static_salt = 'M0AaE|}{<}|{&*#^AhEQ';
$dynamic_salt = mt_rand();
$newRequesterPass = sha1($dynamic_salt . $password . $static_salt);
$newRequesterDynamSalt = $_POST($dynamic_salt);
I get error: Function name must be a string
How do I get this code to $_POST correctly:
$newRequesterDynamSalt = $_POST($dynamic_salt);
$_POST is an array, you access arrays with [], so:
$_POST[$dynamic_salt]
But, it seems you confused server side and client side into one script.
The fix I gave is just for the specific error, not for the algorithm u try to implement.

How do I use a variable as an index to an array in PHP?

I have a config file which has variables based on the domain of the page (HTTP_HOST)
$c is an array.
eg $c['example.com-user'] is an entry in the config file.
I need to pass the value of $c['example.com-user'] to a function building the variable name on the fly.
I have "example.com" in $host variable
Thanks for any help!
<?php
$c['example.com-user'] = "someval";
$host = "example.com";
echo $c[ $host . '-user'];
?>
Tested, working.
$c[$host.'-user']
Assuming you only ever need the config data for the current domain, might it be easier to have a config file per domain, rather than one monster array with all of them? Then you can just load up the relevant file at the start.
If, for example your domain stored in variable $host and username in variable $user. Then, all you have to do is, use this variables as array's key:
echo $c[ $host . "-" . $user];
if $host = 'inform.com' and $user = 'simple_user', the code above will translate into:
echo $c['inform.com-simple_user'];
You see, to concatenate string, you use '.' (dot).

Categories