I have the following hardcoded into jquery and I want to move the code over to pull the values from a database using ajax.
I get the data back and pass it through using json_encode but I need to keep the same format.
codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');
Here is the php array prior to json_encode.
$codes = array();
codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');
I am trying to keep the same format as I do not want to rewrite all the other code in the script. Is it possible to match format?
if I understand right, you need format like this in your ajax responce.
codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');
for PHP you need next:
$codes = array();
$codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
$codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
$codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');
echo 'codes='.json_encode($codes).';';
this is not similar visual, but equal in JS Object structure.
So the issue was in the ajax code, I had code outside of my ajax call that wasn't being called. After I moved the methods inside the success callback of the ajax call it all worked perfectly.
Related
I am passing array from HTML page to PHP using xmlhttp. It all appears to be working fine. However, I need to verify that the PHP is receiving and decoding the JSON properly.
The code to read the JSON in PHP is as follows:
$str_json = file_get_contents('php://input');
$weekdates = json_decode($str_json, true);
$MON = $weekdates[0];
$TUE = $weekdates[1];
$WED = $weekdates[2];
$THU = $weekdates[3];
$FRI = $weekdates[4];
A simple echo in PHP won't work because the PHP script is being called from the HTML page.
So how does one display the variable values from the PHP file to see if what is being sent is what is being received.
I want to get request in a website like that
$id = "something";
$response = http_get("http://example.com?id=$id, array("timeout"=>1), $out);
I don't find how exactly I can use http_get
I've tried with file_get_contents, it is working like
$out = file_get_contents("http://www.example.com?id=something");
but I want it like that
$out = file_get_contents("http://www.example.com?id=$id");
this one is not working
The way I got it to work was using the code below.
$out = file_get_contents("http://www.example.com?id=".$id);
This will concatenate the $id variable contents onto the end of the url to request.
I am new to AS3, and I had tried a few times to pass an array from php to AS3. But i can't manage to do it.
But i managed to narrow down the problem to 1 set of code, so wondering what do i need to change it.
When the function is this
function Asandler(event:Event){
var responseVariables:URLVariables = new URLVariables(event.target.data);
nobed = responseVariables.nobed ;
zip = responseVariables.zip;
Location = responseVariables.Location;
price = responseVariables.price;
}
It returns an error Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
But when i change it to
function Asandler(event:Event){
s1.test.text=event.target.data
}
It displays array with no problem, inside the dynamic text field.
php echo part
$solutions = array();
while ($row = mysqli_fetch_assoc($sql))
{
echo "nobed=".$solutions[1]=$row['nobed'];
echo "&zip=".$solutions[2]=$row['zip'];
echo "&Location=".$solutions[3]=$row['Location'];
echo "&price=".$solutions[4]=$row['price'];
}
Test Data string
nobed=100&zip=100&Location=100&price=100
New try, testing it with dynamic text field, it display the whole string.
var receivedValue:String = event.target.data.replace(/^\s+|\s+$/mg, "");
var test:Array = receivedValue.split(",");
s1.test.text =test[0];
But not too sure how to split the string up.
How can I get a value of a second level JSON node using PHP from a Ajax request?
If I have the next JSON data in client:
var Data = {idJS: "1", dataToSet: "example", another:{ field1: "example2"} };
When the Ajax request is done, in PHP, dataToSet is get as follows: $_POST['dataToSet'], in particular, I use Codeigniter, then I use $this->input->post('dataToSet'). But, How can I get another->field1?
[Solved]:
$postdata = $this->input->post();
$postdata['another']['field1'];
I don't know how you do it with codeigniter, but you have to decode the json. Afterwards you can acces the field. This might look something like this:
$postdata = json_decode($this->input->post);
$postdata['another']['field1];
This should do it:
$data = $this->input->post('another');
print_r($data->field1);
// or you can do it in one line:
$this->input->post('another')->field1;
Try this:
$ata = json_decode($this->input->post('dataToSet'), true);
$field1 = $data['another']['field1'];
I just started learning PHP, and have been trying to build a website to learn. I found a javascript script that rotates text on the internet here which looks like:
<script language="JavaScript">
function rotateEvery(sec)
{
var Quotation=new Array()
// QUOTATIONS
Quotation[0] = 'First quotation';
Quotation[1] = 'Second quotation';
Quotation[2] = 'Third quotation';
Quotation[3] = 'Fourth quotation';
Quotation[4] = 'Fifth quotation';
Quotation[5] = 'Sixth quotation';
Quotation[6] = 'You can add <b>as many</b> quotations <b>as you like</b>';
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}
</script>
I also have a database table called events that has three fields ( id, when, tag) When is a date, tag is the description of the event (e.g Christmas Party/Halloween at my house).
What i am trying to do is select the events that are happening today and put them in my javascript rotator, randomly.
Is this possible? How would I go about implementing this? I know I am really bad at explaining my questions so if I left out any more details if you could just tell me and I can help.Thanks!
So if I understand your intent, you want to pull events from your database and pass them into the JavaScript on your page to use in your rotator:
In your PHP
Use whichever MySQL API you are using already to execute the query. Using the old mysql_*() functions would look like the following. (Note: use of the mysql_*() functions is actually NOT recommended, but it seems most likely that's what you're currently using. I'll update if I find out otherwise...)
// Assuming `when` is a real DATE or DATETIME data type in MySQL...
// compare to CURDATE() to get today's
$result = mysql_query("SELECT tag FROM events WHERE DATE(when)=CURDATE()");
if ($result) {
// array to hold all the output
$events = array();
while ($row = mysql_fetch_assoc($result)) {
// Add the event to your array
$events[] = $row['tag'];
}
// After building the array, encode it as JSON
// Later you'll echo this into your JavaScript in place of the array...
$events = json_encode($events);
}
Later in your HTML/JavaScript output
Output the JSON string (your array) into the JavaScript on your page:
function rotateEvery(sec)
{
// The JSON from PHP output here
// Would look something like
// ["Event 1","Event 2","Event 3"]
var Quotations = <?php echo $events; ?>;
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}