Custom form posting "Array" rather than user input values - php

We are using the Aheadworks Helpdesk Module and are trying to create a second form to capture specific information and use the form to create a ticket where all of the form content gets posted to the "content" section of Helpdesk.
The problem is, if I use the name="content", what gets posted into the "content" section is simply "Array"
The form code is quite simple:
<form id="helpdesk-ticket-form" action="../helpdeskultimate/customer/new/" method="post" enctype="multipart/form-data"><input name="title" value="WHOLESALE SETUP REQUEST" type="hidden">
<div><label for="title_field">Name<span class="required">*</span></label><br> <input id="title" class="input-text required-entry" style="width: 250px;" name="" value="" type="text"></div>
<div> </div>
<div><label for="title_field">Company Name<span class="required">*</span></label> <br><input id="content_field" class="input-text " title="Company" name="content" value="" type="text"></div>
<input name="department_id" value="2" type="hidden">
<div> </div>
<div><label for="content_field">Message<span class="required">*</span></label><br> <textarea id="content_field" class="required-entry" style="width: 450px;" name="content" rows="10" cols="53"></textarea></div>
<div> </div>
<div> </div>
<div><label for="filename">Attach Reseller Permit (2Mb limit)</label><br> <input id="filename" class="input-file" style="width: 450px;" name="filename" type="file"></div>
<div class="button-set"> </div>
<div class="button-set"><span><span><br></span></span></div>
<div class="button-set"><button class="button right form-button" type="submit"><span> <span>Submit ticket</span></span> </button></div>
</form>
I have tried using name="content[]" but it also returned "Array"
The module looks to be using this post method:
public function newAction()
{
if (!$this->_getCustomerSession()->getCustomerId()) {
$this->_getCustomerSession()->authenticate($this);
return;
}
$session = Mage::getSingleton('core/session');
$customer = $this->_getCustomerSession()->getCustomer();
$Proto = Mage::getModel('helpdeskultimate/proto');
$postData = $this->getRequest()->getPost();
if (isset($postData['department_id'])) {
$Proto->setDepartmentId($postData['department_id']);
}
try {
$Proto
->setSubject(#$postData['title'])
->setContent(#$postData['content'])
->setPriority(#$postData['priority'])
->setStoreId(Mage::app()->getStore()->getId())
->setFrom(Mage::getSingleton('customer/customer')->getId())
->setSource('web');
The insert into the message field seems to come from here:
/* Insert */
try {
$message->setContent(trim($data['content']));
$validateResult = $message->validate();
The full controller file can be downloaded from http://www.gingabox.com/CustomerController.zip
I am not sure how to actually use a foreach statement with the #$postData['content'], or if there is a better solution.
I would happily ask AheadWorks, but have been told by them that they are not accepting customization inquiries at this time (too busy)...
Any help would be greatly appreciated!

The word "Array" is what you get when you convert a PHP array into a string; since array values can contain anything, PHP doesn't bother trying to figure out how to convert an array and just returns the string "Array". This is exactly what happens in the line:
// The trim() function casts $data as a string => string(5) "Array"
$message->setContent(trim($data['content']));
There are functions that do return a string representation of array contents, such as print_r(). This will spit out the array in a multiline string, so incorporating that in your code would be:
$message->setContent(print_r($data['content'], TRUE));
If you wanted the contents of the array as a single-line string you should probably use a foreach() statement like you mentioned in your question. Here's a quick example:
$contentString = 'Second Form values:' . PHP_EOL;
foreach($data['content'] as $key => $value) {
$contentString .= PHP_EOL . ' ' . $key . ': ' . $value;
}
Then you would be able to use $contentString as the message instead of accessing the $data array value directly. I don't know what the validate() method is doing in your example, but it is definitely a good idea to ensure that you are properly escaping the values within this second form before you use them as the body of an email.

If you can change the form definition in HTML then maybe you will be able to receive an array as content, please have a look at the following example:
<form id="helpdesk-ticket-form" action="tescik.php" method="post" enctype="multipart/form-data"><input name="title" value="WHOLESALE SETUP REQUEST" type="hidden">
<div><label for="title_field">Name<span class="required">*</span></label><br> <input id="title" class="input-text required-entry" style="width: 250px;" name="content[name]" value="" type="text"></div>
<div> </div>
<div><label for="title_field">Company Name<span class="required">*</span></label> <br><input id="content_field" class="input-text " title="Company" name="content[company]" value="" type="text"></div>
<input name="department_id" value="2" type="hidden">
<div> </div>
<div><label for="content_field">Message<span class="required">*</span></label><br> <textarea id="content_field" class="required-entry" style="width: 450px;" name="content[message]" rows="10" cols="53"></textarea></div>
<div> </div>
<div> </div>
<div><label for="filename">Attach Reseller Permit (2Mb limit)</label><br> <input id="filename" class="input-file" style="width: 450px;" name="filename" type="file"></div>
<div class="button-set"> </div>
<div class="button-set"><span><span><br></span></span></div>
<div class="button-set"><button class="button right form-button" type="submit"><span> <span>Submit ticket</span></span> </button></div>
</form>
Please note the changed form names like content[message], content[company] etc. This should resolve to an Array of values.

Related

php search form using post variable to make an sql query

this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>

PHP: Can't escape characters in for inputs

I'm new to this PHP thing. Trying to escape some php script in a form and can't figure out how to do it.
I've tried all different combinations of \' and \" but I'm stuck. Please the relevant portion of script and advise what I'm doing wrong.
This is the example of input:
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
And this is the rest of the display/redisplay part of the script so you can see it in context...
//Redisplay/Display form
$self = htmlentities($_SERVER['PHP_SELF']);
$displayOutput .= '
<form action="' . $self . '" method="post">
<fieldset>
<div>
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
</div>
<div>
<label for="email"> Email :</label>
<input type="email" name="email" id="email" />
</div>
<div>
<label for="mailFormat">Mail Format</label>
<select name="mailFormat" id="mailFormat">
<option value="plain">Plain text</option>
<option value="html">HTML</option>
</select>
</div>
<div>
<input type="checkbox" name="terms" id="terms" value="yes" />
<label for="terms">Tick this box to confirm you have read our terms and conditions</label>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</fieldset>
</form>';//close form
try to change this line of code
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
to
<input type="text" name="fullName" id="fullName" value="'.$C_fullName.'" />
and apply this change at other places
The problem is that you are trying to escape into PHP evaluation from within PHP. That doesn't make sense.
When a document is parsed, it is interpreted as normal, verbatim text until the interpreter hits an escape sequence like <?php. Everything until the closing ?> (or till the end of file) is interpreted as PHP. That's why putting another <?php opening sequence into your code is nonsensical.
The form markup in above code is build as a PHP string. What you are trying to do is to create a string and concatenate a variable into it. PHP offers multiple ways to do this. The simplest, in your case, is to use the concatenation operator .. Close the string, concatenate the variable, and concatenate the rest of the string.
… value="' . $C_fullName . '" …
It's no different than the action definition in the first line.
You might also want to look at the heredoc syntax, it can be quite useful for situations like yours.
You can give a try with the following code :
$displayOutput .= '
<form action="' . $self . '" method="post">
<fieldset>
<div>
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value='; echo $C_fullName; $displayOutput .=' />
</div>
<div>
<label for="email"> Email :</label>
<input type="email" name="email" id="email" />
</div>
<div>
<label for="mailFormat">Mail Format</label>
<select name="mailFormat" id="mailFormat">
<option value="plain">Plain text</option>
<option value="html">HTML</option>
</select>
</div>
<div>
<input type="checkbox" name="terms" id="terms" value="yes" />
<label for="terms">Tick this box to confirm you have read our terms and conditions</label>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</fieldset>
</form>';

Using echo to print multiple forms in html with PHP

So I have this code that is supposed to print a bunch of boxed in results, that should function as forms to get any user's data, however when I store a huge html string into a variable to then print onto my html, it only appends the form tags once on one of the boxes. Why is this happening? It does not seem logical to me. This is what I have done with my code:
<?php
if($searchData == "" || $resultArray == null){
echo "<h2>No results found<h2>";
} else {
foreach($resultArray as $iterator){
$finalResult = $finalResult.'<div class="col-lg-4 col-sm-6">
<div class="properties">
<form action="user-detail.php" method="POST">
<h4>'.$iterator['USERNAME'].' </h4>
<h5>'.$iterator['PERSON_NAME'].'</h5>
<h5>'.$iterator['FIRST_LAST_NAME'].'</h5>
<h5>'.$iterator['SECOND_LAST_NAME'].'</h5>
<input class="form-control" type="text" style="display: none" readonly name="username" value="'.$iterator['USERNAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="p_id" value="'.$iterator['PERSON_ID'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="name" value="'.$iterator['PERSON_NAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="last_name" value="'.$iterator['FIRST_LAST_NAME'].'"/>
<input class="form-control" type="text" style="display: none"readonly name="second_last_name" value="'.$iterator['SECOND_LAST_NAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="b_value" value="'.$iterator['BLACKLIST'].'"/>
<input type="submit" class="btn btn-primary" value="View Details"
<form/>
</div>
</div>';
}
echo $finalResult;
}
?>
Normally, I wouldn't ask for help with this, but I have no clue why an error like this could happen. It makes no sense to me as to why this would happen. I would greatly appreciate if someone could clarify as to why this happens.
Your ending form tag is improperly formatted.
You have: <form/>
It should be: </form>
I hope that helps!

Sending Values to file_get_contents() [duplicate]

This question already has an answer here:
PHP Using File_get_Contents() to pre populate a form [closed]
(1 answer)
Closed 7 years ago.
I am making a site that uses the file_get_contents to display a form. The form contains three fields and is as below. What I am trying to do is insert three values that I have generated from sql queries into this form.
Is there a way that I can send the values I have generated into the correct fields of the form?
Would I have to add some php to the top of the form that will allow it receive the values being passed from the file_get_contents function?
Calling the functions and form
//sql functions return values based on rowID
$updateTilte = $this->model->updateTitle();
$updatePrice = $this->model->updatePrice();
$updateDescription = $this->model->updateDescription();
$rightBox = file_get_contents( "templates/update_item_form.php" );
Update_item_form
<h2>Update Item!</h2>
<h4>Fill in the form to update an entry.</h4>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='updateItem' />
<p>
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required />
</p>
<p>
<label for="fPrice">Price</label> <input type="text"
id="fPrice" name="fPrice" placeholder="price"
maxlength="25" required />
</p>
<p>
<label for="fDescription">Description</label> <input type="text"
id="fDescription" name="fDescription" placeholder="description"
maxlength="500" required />
</p>
<p>
<div class="form-group">
<div class="controls">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</p>
</fieldset>
</form>
No. That's not what file_get_contents does, and it's not what it's for either. What you are looking for is include
It opens the file in PHP mode, executing whatever code it finds. That means you can use the variables you declared above inside the file, and PHP will replace them with their value.
Snippets:
$updateTilte = $this->model->updateTitle();
include "templates/update_item_form.php";
Form:
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required value="<?php echo $updateTilte; ?> />
Note: include will immediately output the html in the file. Only include the file when you're ready to output everything.

Codeigniter $_SERVER['PHP_SELF'] not working in views

I have a dialog form.
<div id="dialog-form" title="Create new Admin">
<p class="validateTips">All form fields are required.</p>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" class="text ui-widget-content ui-corner-all" />
<label for="role">Role</label>
<select name="user_role" class="select ui-widget-content ui-corner-all" >
<option value="administrator">Administrator</option>
<option value="visitor">Visitor</option>
<option value="Helper">Helper</option>
</select>
<label for="email">Email</label>
<input type="text" name="login_email_admin" id="login_email_admin" value="" class="text ui-widget-content ui-corner-all" />
<label for="Passoword">Password</label>
<input type="passowrd" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
<label for="Passoword">Re-Enter Password</label>
<input type="password" name="password_2" id="password_2" value="" class="text ui-widget-content ui-corner-all" />
<input style="margin-top:15px;" type="submit" name="add_admin" value="Add New Admin">
</fieldset>
</form>
i am using following php code in views to take data from this dialog form when it will be submitted..
<?php
if($_POST['add_admin'])
{
$this->user_role=$this->input->post('name');
$this->user_role=$this->input->post('user_role');
$this->login_email_admin=$this->input->post('login_email_admin');
$this->password=$this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$this);
}
?>
But it's not inserting into the db.this problem really stuck me.i am calling page itself when form submit, i dont know what is the reason its not working.
additionally is there a way that i will get rid of using models, instead i can do all db operations in views?
Add an echo to print the variable ;)
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
This code is wrong in many aspects:
if($_POST['add_admin'])
{
$this->user_role=$this->input->post('name');
$this->user_role=$this->input->post('user_role');
$this->login_email_admin=$this->input->post('login_email_admin');
$this->password=$this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$this);
}
You're overwriting the user_role property;
you're passing the whole $this reference, which contains way much more than those properties
that's not how you do an insert with Active Record! Field names must be passad as an array, not as object properties
Should be something like:
$field['user'] = $this->input->post('name'); //or whatever is the FIELD NAME
$field['user_role'] = $this->input->post('user_role');
$field['login_email_admin'] = $this->input->post('login_email_admin');
$field['password'] = $this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$field);
See insert chapter on manual for reference.
Also, I don't know why you want to do that inside a view, you should do the insert in a model, and the checking for the form being submitted must be done in the controller (ideally you could use the Form Validation class, which is very handy for this task.
You're using a framework with an MVC architecture but in this piece of code you're taking advantage of almost nothing from both...
1) You need to pass the $_POST to your view, in the controller...
$this->load->view("MyView",array('_POST'=>$_POST));
2) You wont need PHP for your solution to post to self
<form action="" method="post" enctype="multipart/form-data">
Browser default when action is blank is self. An alternative (more absolute) solution could also be $_SERVER['REQUEST_URI']
If you wanted to be more CI, you could do this inline (with validation class instantiated and form helper included)
<?php echo form_open(''); ?>
Additionally $_SERVER['PHP_SELF'] wont work because you're running from index.php, your URL is controlled via .htaccess.
This question has been answered already, but I want to point out that form_open() without any arguments would do exactly what you want (creating an action="").
So you can simply use the snippet below:
<?php echo form_open(); ?>
Here is a reference from the CodeIgniter's Source:
function form_open($action = '', $attributes = '', $hidden = array())

Categories