PHP_Perl_Detailed_Answers
PHP_Perl_Detailed_Answers
What is AJAX?
AJAX (Asynchronous JavaScript and XML) allows updating web pages without refreshing. It uses
`XMLHttpRequest` to send/receive data asynchronously.
- **Basic Functions**:
- `abs($num)`: Returns absolute value.
- `ceil($num)`: Rounds up to the nearest integer.
- `floor($num)`: Rounds down to the nearest integer.
- **Example**:
```php
echo abs(-10); // 10
echo ceil(4.2); // 5
echo floor(4.9); // 4
```
- **If-Else Statement**:
```php
$num = 10;
if ($num > 0) {
echo "Positive";
} else {
echo "Negative";
}
```
Example:
```php
function customError($errno, $errstr) {
echo "Error: [$errno] $errstr";
}
set_error_handler("customError");
echo $undefinedVar;
```
How can we connect a database using PHP and AJAX? Explain with an example.
**Steps**:
1. Create an HTML form.
2. Use AJAX to send data.
3. Process request in PHP.
**Example:**
**index.html**
```html
<input type='text' id='name'>
<button onclick='sendData()'>Submit</button>
<script>
function sendData() {
let name = document.getElementById('name').value;
let xhr = new XMLHttpRequest();
xhr.open('POST', 'process.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('name=' + name);
}
</script>
```