Friday, 1 February 2019

php - jquery ajax post doesn't work

The jQuery code is sending the POST data as a JSON string, but your PHP code is looking for standard key-value pairs.



You can change the Ajax call to send in key-value format. I can't see what utnew refers to in your code, usually that would be a Javascript object or the result of serializing an HTML form with $.serialize():




$.post("myurl.php", utnew, "json")
.always(function(response) { console.log(response); }


Or you can change the PHP side to expect a JSON-encoded string in the POST payload rather than encoded key-value pairs:



    ...
$data = json_decode(file_get_contents('php://input'));

if (isset($data['id'])) { ... }


Whichever you choose, the problem you're having is the Ajax call sending a JSON-encoded string as the POST payload, but PHP expecting key-value pairs in $_POST.



The "json" data type argument to jQuery $.ajax or the shortcut method $.post tells jQuery what format to expect the return value in, so your PHP code should return a JSON-encoded string. Your PHP code is sending back a plain string, so you should either tell jQuery to expect that:



$.post(url, data, "text")



or have PHP send back a proper JSON-encode object:




echo json_encode(["result" => "success"]);



If you have PHP error logging on (you should) to a file you can debug your API endpoint with error_log, i.e. error_log(print_r($_POST,1)); would have given you some clues.

c++ - Why couldn't push_back be overloaded to do the job of emplace_back?




Firstly, I'm aware of this question, but I don't believe I'm asking the same thing.



I know what std::vector::emplace_back does - and I understand why I would use it over push_back(). It uses variadic templates allowing me to forward multiple arguments to the constructor of a new element.



But what I don't understand is why the C++ standard committee decided there was a need for a new member function. Why couldn't they simply extend the functionality of push_back(). As far as I can see, push_back could be overloaded in C++11 to be:



template 
void push_back(Args&&... args);



This would not break backwards compatibility, while allowing you to pass N arguments, including arguments that would invoke a normal rvalue or copy constructor. In fact, the GCC C++11 implementation of push_back() simply calls emplace_back anyway:



  void push_back(value_type&& __x)
{
emplace_back(std::move(__x));
}


So, the way I see it, there is no need for emplace_back(). All they needed to add was an overload for push_back() which accepts variadic arguments, and forwards the arguments to the element constructor.




Am I wrong here? Is there some reason that an entirely new function was needed here?


Answer



If T has an explicit conversion constructor, there is different behavior between emplace_back and push_back.



struct X
{
int val;
X() :val() {}
explicit X(int v) :val(v) {}

};

int main()
{
std::vector v;
v.push_back(123); // this fails
v.emplace_back(123); // this is okay
}



Making the change you suggest would mean that push_back would be legal in that instance, and I suppose that was not desired behavior. I don't know if this is the reason, but it's the only thing I can come up with.


arrays - MATLAB matrix element wise multiplication optimization

Given that the arrays in the input cell arrays are of identical sizes, it might be a better idea to have the inputs stored as multi-dimensional arrays instead of cell arrays to leverage MATLAB's vectorized techniques, which in this case would be indexing for extracting specific elements and matrix-multiplication for sum-reduction. So, when forming the inputs, we could look to form multi-dimensional arrays corresponding to the inputs : B_Container_Cell, C_Container_Cell, Coeff_x_Cell, Coeff_y_Cell and Coeff_z_Cell. Now, these are 1D cell arrays with B_Container_Cell containing 2D arrays and rest have 3D arrays. Thus, when using multi-dimensinal arrays, we would have them as one additional dimension, i.e. they would be 3D and 4D arrays respectively.


To simulate their multi-dimensional array formats, let's convert the given cell arrays with concatenation using cat along their last+1 dimension, like so -


Bm = cat(3,B_Container_Cell{:});
Cm = cat(4,C_Container_Cell{:});
Cx = cat(4,Coeff_x_Cell{:});
Cy = cat(4,Coeff_y_Cell{:});
Cz = cat(4,Coeff_z_Cell{:});

Finally, the vectorized solution to use these multi-dimensional arrays and get the desired outputs -


%// Get ACB across all iterations and reshaped into (Nx28) shaped array
Ar = reshape(bsxfun(@times,bsxfun(@times,Cm,permute(Bm,[1,2,4,3])),A_MAT),[],28);
%// Use matrix-multiplication to sum reduce sliced versions of Cx, Cy and
%// Cz, to get respectived summed outputs
sz = size(A_MAT); %// Output array size
Sum_x_out = reshape(Ar*reshape(Cx(p1,p2,:,:),[],1),sz);
Sum_y_out = reshape(Ar*reshape(Cy(p1,p2,:,:),[],1),sz);
Sum_z_out = reshape(Ar*reshape(Cz(p1,p2,:,:),[],1),sz);

Please note that it doesn't look like the parameter p3 was used.


Runtime test results (for listed sample inputs) -


--------------------------------- With Original Approach
Elapsed time is 2.412417 seconds.
--------------------------------- With Proposed Approach
Elapsed time is 1.572035 seconds.

html5 - What does the value attribute mean for checkboxes in HTML?



Suppose this checkbox snippet:




Is it worth?


Is there any reason to statically define the value attribute of checkboxes in HTML? What does it mean?


Answer



I hope I understand your question right.



The value attribute defines a value which is sent by a POST request (i.e. You have an HTML form submitted to a server).
Now the server gets the name (if defined) and the value.





Is it worth?



The server would receive mycheckbox with the value of 1.



in PHP, this POST variable is stored in an array as $_POST['mycheckbox'] which contains 1.


jquery - Find object by id in an array of JavaScript objects



I've got an array:




myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]


I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that object in the array.



How do I do this in JavaScript or using jQuery?


Answer



Use the find() method:




myArray.find(x => x.id === '45').foo;


From MDN:




The find() method returns the first value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.








If you want to find its index instead, use findIndex():



myArray.findIndex(x => x.id === '45');


From MDN:




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.








If you want to get an array of matching elements, use the filter() method instead:



myArray.filter(x => x.id === '45');


This will return an array of objects. If you want to get an array of foo properties, you can do this with the map() method:




myArray.filter(x => x.id === '45').map(x => x.foo);





Side note: methods like find() or filter(), and arrow functions are not supported by older browsers (like IE), so if you want to support these browsers, you should transpile your code using Babel (with the polyfill).


performance - Why is the Android emulator so slow? How can we speed up the Android emulator?

I have got a 2.67  GHz Celeron processor, and 1.21  GB of RAM on a x86 Windows XP Professional machine.



My understanding is that the Android Emulator should start fairly quickly on such a machine, but for me, it does not. I have followed all the instructions in setting up the IDE, SDKs, JDKs and such and have had some success in starting the emulator quickly, but that is very rare. How can I, if possible, fix this problem?



Even if it starts and loads the home screen, it is very sluggish. I have tried the Eclipse IDE in version 3.5 (Galileo) and 3.4 (Ganymede).

only change css file of yii widget, where to save this file

I want to make changes to a css file of a widget (Yii). Where do I have to put only the changed css if i do not want to change the core files?

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...