Codeigniter Preg_match error - php

I am trying to implement a search function that will use ajax to query the database when a user types a character into the search box This will bring up any data within the database that begins with the character typed by the user.

I think this line, is your problem;
$this->load->view(array('words' => $wordlist));
You are trying to load an array of views.
The first parameter within the view function, is the name of the file you're trying to load. Like this;
$this->load->view('file_name', $data, true/false)
https://ellislab.com/codeigniter/user-guide/libraries/loader.html

If you want a specific type of data and simple out, you must use header response without $this->load->view just like this:
header('Content-type: application/json');
echo json_encode(array('words' => $wordlist));
Use that, you can easily get data with ajax

Related

Is it possible to send an array in a php header (like you can with a form)?

I am trying to make a dynamically sized form for a web-page I am creating! I have had no issue passing the information needed to the 'action' page through a form (including two arrays), by setting the name of all dynamically created forms to be name[i].
To get the data from the array in the 'action' file, I use the code below, and it works fine:
$_POST['name'][$i]
However, I wish to return the information to the form if there is an error with any of it, and the way I am doing this is with headers.
header("Location: ../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".$someArray);
exit();
Is there anything I need to change for this to return something other than Array()?
Clearly the header is using the $_GET method rather than the form's $_POST method, but why can I only send the array one way?!
Any help would be appreciated!
The issue you have is that you try to concatenate your array to a string, but that does not happen in the way you would prefer. You could convert your array into JSON, like this:
../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".json_encode($someArray));
Read more about json_encode by clicking on the link.

How can I get array from GET request?

I have a trouble:
I have url of view:
?materials=1&materials=2&materials=3&materials=4&materials=5&materials=6
How can I get it in array throw Yii mechanism?
Yii::app()->request->getParam('materials')
Not working
$_GET['materials']
Too not working
Url is static and can not change
This URL was created https://github.com/Mikhus/jsurl plugin
PHP automatically parses parameter as an array, if it ends by []. (i.e. ?materials[]=1&materials[]=2&materials[]=3&materials[]=4&materials[]=5&materials[]=6) Otherwise you can parse query string manually to solve your issue. Look at this

Replicating preloaded HTML /DOM method results in an array using AJAX/PHP

I have a function that creates an array that contains the return value from the HTML DOM method : window.document.getElementById()
function makearray1(){
var array1=[1,window.document.getElementById('divID'),['a','b'],[1,2]];
}
then I pass the array into another function
use(array1)
function use(xxx){
xxx[1].innerHTML=xxx[2][0];
}
and 'a' is written in the appropriate div
later I decided to put the array in a form and post it to a txt file on the server using php and:
JSON.stringify(array)
So now I use AJAX to call the data from the txt file after the rest of the page has loaded etc... and the original function used to make the array is not included at all.
so my php is basically this:
$a1='use(';
$data1 =file_get_contents("text_file.txt") ;
$a2=')';
echo $a1.$data1.$a2;
and the response text:
var n= XMLHttpRequestObject.responseText;
eval(n);
which pretty much means this:
use(text_file)
function use(xxx){
xxx[1].innerHTML=xxx[2][0];
}
the problem is that the array in the text file looks like this:
[1,null,['a','b'],[1,2]]
instead of:
[1,window.document.getElementById('divID'),['a','b'],[1,2]]
My question: Is there any way that I can do the equivalent of what I'm trying to do here, which is immediately replicate the return value of the HTML/DOM method in an array using AJAX/php?
To clarify: this is a simple example. I actually have a huge, multidimensional array that already has established pointers, or prefetched DOM nodes in it. Now I'm trying to replicate the array when a text version is loaded using ajax. I'm looking for a recursive approach to changing all of the null assignments with something that will immediately fetch the appropriate DOM node. Most likely I will need to do it with the response text, but was hoping I could do it with the php portion.
You're trying to stringify a reference to a javascript object in the memory of whatever computer is evaluating getElementById first, and that has no chance to represent something on the end client's computer.
Send the id instead:
function makearray1(){
array1=[1,'divID',['a','b'],[1,2]];
}
then, in the client:
function use(xxx){
window.document.getElementById(xxx[1]).innerHTML=xxx[2][0];
}
If you really want to eval it at the end, you can use this, I guess
function makearray1(){
array1=[1,"window.document.getElementById(\"divID\")",['a','b'],[1,2]];
}
I've no idea why you would want to do that though
Assuming the dom element exists in the second page, it should look something like this.
JS:
function packDomData(){
return {
"MySpecificYetBriefProperty0":1,
"DomID":"divID",
"MySpecificYetBriefProperty1":['a','b'],
"MySpecificYetBriefProperty2":[1,2]
};
}
function useDomData(domData){
document.getElementByID(domData.DomID).innerHTML=domData.MySpecificYetBriefProperty1[0];
}
PHP:
//Make sure the contents of this file is the json from packDomData. use json_encode in php or JSON.stringify in js
echo file_get_contents("text_file.txt");
var myData = JSON.parse(XMLHttpRequestObject.responseText);
useDomData(myData);
I used to code like you. Here are some tips that have helped turn my coding horror into a fun job:
Use objects with descriptive properties instead of arrays whenever you aren't actually looping through the array - I promise it will save you and others headache! "DomID" is much more flexible than 1, and if you change the order in the array, javascript gods help you that array is everywhere - including however many random text files on your server!
Also use descriptive names for functions
Always return a value from a function instead of using globals whenever possible, even where the result is then used as a nasty global. Trust me.
Never put javascript function names in an ajax call. Use JSON instead and keep the functions and other script in the javascript file where it belongs.
Mysql will save your life!!
Disclaimer - I didn't run this code but it should basically work when you get everything hooked up right.

Yii Framework - beginWidget without echo

I use this code to create my form:
$form=$controller->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableAjaxValidation'=>false
));
When I do this, it prints the form. But I want to put this in a variable, because the entire form is returned as string in the method. How can I do this?
The following takes the output buffer, stores it into a variable and then erases it. Enjoy!!!
ob_start();
$form=$controller->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableAjaxValidation'=>false
));
$formToStore = ob_get_clean();
You can use the controller's renderPartial method to return a string. Simply put your entire form in a file by itself, let's call it _myForm.php.
Then use this line to write the form contents to the variable $form:
$form = $controller->renderPartial('//stuff/_myForm', $data, true);
The 3rd parameter set to true will return the contents of that renderPartial, instead of echoing. That includes 'overriding' the echoing of the widget itself.
The $data parameter includes any variables you need to pass to the form.
The file itself will need to be in the protected/views/stuff/ folder. The // start to the first parameter is a shortcut for the protected/views/ folder.
Alternately, use just '_myForm' for the first parameter if that file is in the same views folder as where the renderPartial is called from.
Note: this is not really functionally different than the option to use ob_start and ob_get_clean. It is simply the more Yii way to do it. There may be an advantage to one version over the other depending on the specifics of your situation.

accessing array from another page in php

I would like to know how can I access or read the array from another page. I am working on a PHP page which contains the array, and I want to display the content of that array on another PHP page.
For example, I used the following method in a PHP file and I want to get the content of the array in another PHP file. what is the method that is going to receive the array's content in the second page.
<?php
$r = new HttpRequest('http://localhost/sameh.php', HttpRequest::METH_POST);
$r->addPostFields(array("n" => 'heba')) ;
$r->send();
?>
This code is in the first page but I don't know what to write to receive it on the second one.
Maybe my question was not that clear and sorry about that ,, I want to find a way to access the array that is defined inside the HttpRequest() class on another page. So that the array "n" that include value "heba" will be displayed on another.php page. this is what make me thinking that the problem is on how to access the content of the array on the second page.
I tried the session and It sends the array to another page ,, but when I tested with the
httpRequest() method it doesn't send the content of the array "heba" to the second page.
Thanks for your help.
Sounds like this is a job for Sessions.
You can read the complete session guide here
In the script that has the array you can do something like:
session_start();
$_SESSION['array'] = $array;
In the next script you access it similarly:
session_start();
print_r($_SESSION['array']);
Include that file in your php file where you want to use that array. This should solve your issue PHP - How to send an array to another page?
I'm not sure what the HttpRequest class is, but at a guess, it's POSTing variables to the sameh.php file. You should be able to access the variable on the next page by doing this:
echo $_POST['n'];
Which should print "heba".
Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.
I'd use SESSIONs wherever possible.
You can use serialize() and unserialize() on your array to represent it as a string and pass it via POST.

Categories