I am building a form in php that will send the owner an email with the request and link to a page where a table will show all requests. I keep getting "array" in the output with the multiple item checklist... Here is what I have
<form>
<input type="checkbox" name="multimedia[]" value="Assessment" />Assessment<br/>
<input type="checkbox" name="multimedia[]" value="elearning module" />E-Learning Module<br />
<input type="checkbox" name="multimedia[]" value="Photography" />Photography<br />
Video Shoot
Other
<?php
$multimedia = array();
echo implode(',', $_POST['multimedia']);
$multimedia_string = implode(',', $multimedia);
?>
//variables in each cell
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['email'] = $_POST['email'];
$variables['projectTitle'] = $_POST['projectTitle'];
$variables['$multimedia_string'] = $_POST['$multimedia_string'];
$variables['credentialing'] = $_POST['credentialing'];
$variables['description'] = $_POST['description'];
$variables['results_data_page'] = $results_data_page;
Change this:
$variables['$multimedia_string'] = $_POST['$multimedia_string'];
...to:
$variables['multimedia_string'] = implode(',', $_POST['multimedia']);
Explanation:
PHP interprets $_POST['multimedia'] as an array due to having the square brackets [] after the name, as in multimedia[], so you can implode it using the comma as a separator and get a string returned.
There are some other issues, so try this instead:
<?php
//variables in each cell
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['email'] = $_POST['email'];
$variables['projectTitle'] = $_POST['projectTitle'];
$variables['multimedia_string'] = implode(',', $_POST['multimedia']);
$variables['credentialing'] = $_POST['credentialing'];
$variables['description'] = $_POST['description'];
$variables['results_data_page'] = $results_data_page;
?>
Issues:
The first three lines don't seem to do anything.
$multimedia = array(); // never populated
echo implode(',', $_POST['multimedia']);
$multimedia_string = implode(',', $multimedia); // still not populated, so implodes to an empty string.
A couple things here:
$variables['$multimedia_string'] = $_POST['$multimedia_string'];
PHP variables are NOT interpolated in single quoted string. In other words, to translate PHP variables from the variable name $name to the value, in a string, you need to use either double quotes or HEREDOC:
echo "I $emotion you very much.";
$html = <<<EOT
<table>
<tr>
<td>We $emotion camping.</td>
</tr>
</table>
EOT;
So '$multimedia_string' is simply a string containing a dollar sign and the text "multimedia_string".
The other thing is it appears there is no $_POST var with that name or whatever you intended $multimedia_string to translate to.
Related
Im trying to populate the textboxes of a CRUD with the information obtained from a query. I use a stored procedure which already works.
I mainly want to give an ID, click search and populate the rest of the texboxes with the clients rest of information. The rest of the buttons already work so I think my problem is located in this section of code:
if(isset($_POST['btnBuscar']))
{
$opc = "buscar";
$idcliente = ($_POST['txtIdCliente']);
$nombre = ($_POST['txtNombre']);
$apaterno = ($_POST['txtApPat']);
$amaterno = ($_POST['txtApMat']);
$tel = ($_POST['txtTel']);
$email = ($_POST['txtEmail']);
$fecalta = ($_POST['txtFechaAlta']);
$query = "CALL sp_clientes('$opc',$idcliente,'$nombre','$apaterno','$amaterno',$tel,'$email','$fecalta')";
if(mysqli_query($con,$query)){
header("location:Clientes.php?searched=1");
this next part where I have doubts
($_POST['txtIdCliente']) = $reg['IdCliente'];
($_POST['txtNombre'])= $reg['Nombre'];
($_POST['txtApPat']) = $reg['APaterno'];
($_POST['txtApMat']) = $reg['ApMaterno'];
($_POST['txtTel']) = $reg['Telefono'];
($_POST['txtEmail']) = $reg['Email'];
($_POST['txtFechaAlta']) = $reg['FecAlta'];
}
Here is the example. you can achieve more by appending HTML in PHP
echo '<input type="text" style="background-color:blue" id="'.$_POST['txtNombre'].'" name="content'.$_POST['txtNombre'].'" value="'.$_POST['txtNombre'].'"></input>';
I'm using a database to store variables of user names and then access them using php. A form with post method is used to invoke a php file which access the database, retrieves all the user names and then compares them with the entered values. The user names are stored in $row['name'] variable and the name is stored in $photographer variable.
The problem is that there are some characters attached in string of variable $row['name'] when being accessed from db which I can't get rid of.
I thought using these variables directly might be causing some problems so I saved their values in other two variables. But the problem didn't go away.
Below is the php code:
//pin check module
$connection = mysqli_connect("localhost","root","","members") or die(mysqli_error($connection));
$select_query_pin = "SELECT name, pin FROM members";
$select_query_pin_result = mysqli_query($connection, $select_query_pin) or die (mysqli_error($connection));
$total = mysqli_num_rows($select_query_pin_result);
$pin = $_POST['pin'];
$title = explode(",",$_POST['title']);
$url = explode(",",$_POST['url']);
$photographer = $_POST['photographer'];
$genere = explode(",",$_POST['genere']);
$entry = sizeof($title);
while ($total) {
--$total;
$row = mysqli_fetch_array($select_query_pin_result);
$name_sample = $row['name'];
$test_sample = $photographer;
// chop($name_sample);
// chop($test_sample);
preg_replace( "/\r|\n/", "", $name_sample );
// for ($i = 0; $i < strlen($name_sample); $i++) {
// if ($name_sample[$i] == " ")
// echo 'true';
// }
var_dump($name_sample,$test_sample);
if ($name_sample === $test_sample)
echo 'true<br>';
else
echo "false<br>";}
As you can see, I tried using the chop and preg_replace methods but it didn't work. When I check the strings using var_dump, it gives unequal lengths for equal strings. The html view of above code is this:html output
Is there a way to deal with it?
code:
<?php
if(isset($_POST['add_new']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$field = $_POST['field'];
$message = $_POST['message'];
$comment1 =array($_POST['comment1'],$s_date);
$comment2 = $_POST['comment2'];
$status = $_POST['status'];
$s_date = date('Y-m-d');
$interested_in = $_POST['interested_in'];
$academic_details = $_POST['academic_details'];
$city = $_POST['city'];
$sql = "insert into enquires2(name,email,phone,field,message,comment1,comment2,status,s_date,interested_in,academic_details,city,admin_idd)values('$name','$email','$phone','$field','$message','$comment1','$comment2','$status','$s_date','$interested_in','$academic_details','$city','$admin_id')";
$result = mysqli_query($link,$sql);
if($result == true)
{
$msg .= "<p style='color:green;'>You are successfully add new enquiry</p>";
}
else
{
$msg .= "<p style='color:red;'>Error!</p>";
}
}
?>
In this code I want to pass two value in single variable i.e.
$comment1 = array($_POST['comment1'],$s_date);
which show (array) when I print query ($sql). How can I pass two value into single variable ? please help me.
Another option if you don't want to concatenate , use serialize function make an associative array and serialize it and store to db
for example :
$comment1 =serialize(array("comment"=>$_POST['comment1'],"date"=>$s_date));
and when you get form db ,just use
$data = unserialize($yourDataFromDb);
and you get your values like
$data["comment"] // Your comment
$data["date"] // your date
Simply use concatenation
$comment1 = $_POST['comment1'] . $s_date;
But if you want to parse later and keep sepration between comment and date you can use any format like
$comment1 = $_POST['comment1'] . "--date--" . $s_date;
Later you can simply use print_r (explode("--date--",$str));
Something like multivalue field.
You already record the value of $s_date in a separate "date" field, so there's no need to record it again within the comment field.
If you want to combine them for display or reporting purposes later on, then you can easily do that in the object or UI layer using simple string concatenation. I would advise you not to do this when storing the data as you're attempting - otherwise you're just duplicating the same value twice in the row for no obvious reason, as well as making it more difficult to separate what the user actually wrote from the date you inserted into it.
I have a form on a website in which when the page is loaded, it gets all the values for the fields from a database, but when they click submit and the required fields are not filled out, it throws them back and all the data they changed is now reverted to the previous state as if they hadn't touched it at all.
I have decided to put it in variables where if the submit button hasnt been clicked, the variable is the result from the database, but if it has been clicked the variable is the $_POST of the form that has been "submitted".
My question is, how do I make this look "neater" in the code as currently its just 20+ variables, defined twice, in an if statement:
if(!isset($_POST['submit']))
{
$jobtitle = $record['job_title'];
$sitecontact = $record['site_contact'];
$siteph = $record['site_phno'];
$siteadd = $record['site_add'];
$sitesuburb = $record['site_suburb'];
$sitepc = $record['site_pc'];
$jobref = $record['job_ref'];
$quoteref = $record['quote_ref'];
$systembrand = $record['cctv_alarm_brand'];
$jobtype = $record['job_type'];
$datebooked = $record['date_booked'];
[...]
}else{
$jobtitle = $_POST['title'];
$sitecontact = $_POST['cname'];
$siteph = $_POST['cnum'];
$siteadd = $_POST['address'];
$sitesuburb = $_POST['suburb'];
$sitepc = $_POST['postcode'];
$jobref = $_POST['jobref'];
$quoteref = $_POST['quoteref'];
$systembrand = $_POST['brand'];
$jobtype = $_POST['job_type'];
$datebooked = $_POST['date'];
$timebooked = $_POST['time'];
[...]
}
Any advise is greatly appreciated.
Here's how I would do it:
Change the name of the variables so they use the same column names in your database. This will make your code less confusing too in the future for others and for yourself.
Import variables used in the array using extract().
Code:
// turn $record['name'] to $name. be careful because this will overwrite variables
extract($record);
$jobtitle = $jobtitle ?: $_POST['jobtitle'];
Use operator ?
If Condition is true ? then value X : otherwise value Y
$jobtitle = (isset($_POST['submit'])) ? $_POST['title'] : $record['job_title'];
[...]
I have a form with some input texts. It's counted with the name + an id. Like:
megnevezes_1
megnevezes_2
My form has also a counted id tag, called tid_1 and go on.
When I post my form i made a hidden input called darab, which counts how many id's I have.
Then I do the mysql query:
for($k=1; $k=$darab; $k++){
$command = <<<HTML
UPDATE
$dbtablename_template_tetelek
SET
vamtarifa_szj = '$vamtarifa_szj_$k',
megnevezes = '$megnevezes_$k',
me_egyseg = '$me_egyseg_$k',
mennyiseg = '$mennyiseg_$k',
afa = '$afa_$k',
egyseg_ar = 'str_replace(".","",$egyseg_ar_$k)'
WHERE template_id = '$tid_$k'
HTML;
mysql_query($command,$kapcsolat) or die(mysql_error(). $command);
}
But theres something wrong with it. How to attach to my strings the $k string with _? And how to make the str replace in the query?
Try this..
for($k=1; $k=$darab; $k++){
$blah = $egyseg_ar . '_' . $k;
$replace = str_replace(".", "", $blah);
$command = <<<HTML
UPDATE
$dbtablename_template_tetelek
SET
vamtarifa_szj = '$vamtarifa_szj_$k',
megnevezes = '$megnevezes_$k',
me_egyseg = '$me_egyseg_$k',
mennyiseg = '$mennyiseg_$k',
afa = '$afa_$k',
egyseg_ar = $replace
WHERE template_id = '$tid_$k'
HTML;
mysql_query($command,$kapcsolat) or die(mysql_error(). $command);
}
You should let PHP know precisely what it needs to parse. To help it you can use curly brackets like in '{$var1}_{$var2}'
So e.g. mennyiseg = '$mennyiseg_$k' might need to be mennyiseg = '{$mennyiseg}_{$k}' if you already have a variable named $mennyiseg in your code above the loop.