FORMAS DE PASAR PARAMETROS CON WINDOW.LOCATION.HREF Y MOSTRAR EN OTRO FORMULARIO PHP
pasar parametros con Window.location.href mostrar en archivo php
To pass parameters using Window.location.href and display them in a PHP file, you can append the parameters to the URL as query parameters and then access them in the PHP file using the $_GET superglobal variable.
Here's an example of how you can do it:
In your JavaScript file:
javascript
Copy code
const id = 123;
const name = 'John Doe';
window.location.href = '/path/to/your/php/file.php?id=' + id + '&name=' + encodeURIComponent(name);
In your PHP file:
php
Copy code
<?php
$id = $_GET['id'];
$name = $_GET['name'];
echo "ID: " . $id . "<br>";
echo "Name: " . $name;
?>
In this example, we're passing two parameters, id and name. We're encoding the name parameter using encodeURIComponent to make sure that any special characters in the name don't interfere with the URL structure.
In the PHP file, we're accessing the values of the id and name parameters using the $_GET superglobal variable and displaying them using the echo statement.
You can modify the example to pass different parameters and access them in your PHP file as needed.
ahora con el metodo post ejemplo
Sure, here's an example of how to pass parameters using a POST request and display them in a PHP file:
In your JavaScript file:
go
Copy code
const id = 123;
const name = 'John Doe';
const formData = new FormData();
formData.append('id', id);
formData.append('name', name);
fetch('/path/to/your/php/file.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error:', error);
});
In your PHP file:
php
Copy code
<?php
$id = $_POST['id'];
$name = $_POST['name'];
echo "ID: " . $id . "<br>";
echo "Name: " . $name;
?>
In this example, we're passing two parameters, id and name, using a POST request. We're creating a new FormData object and appending the values of the parameters to it using the append method. We then pass the FormData object as the body of the fetch request.
In the PHP file, we're accessing the values of the id and name parameters using the $_POST superglobal variable and displaying them using the echo statement.
You can modify the example to pass different parameters and access them in your PHP file as needed.
pasar parametros con Window.location.href con metdo post y mostrarlo en archivo php
You cannot use Window.location.href to make a POST request. Instead, you can create a form dynamically and submit it using JavaScript to make a POST request. Here's an example:
In your JavaScript file:
javascript
Copy code
const id = 123;
const name = 'John Doe';
const form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', '/path/to/your/php/file.php');
const idField = document.createElement('input');
idField.setAttribute('type', 'hidden');
idField.setAttribute('name', 'id');
idField.setAttribute('value', id);
form.appendChild(idField);
const nameField = document.createElement('input');
nameField.setAttribute('type', 'hidden');
nameField.setAttribute('name', 'name');
nameField.setAttribute('value', name);
form.appendChild(nameField);
document.body.appendChild(form);
form.submit();
In your PHP file:
php
Copy code
<?php
$id = $_POST['id'];
$name = $_POST['name'];
echo "ID: " . $id . "<br>";
echo "Name: " . $name;
?>
In this example, we're passing two parameters, id and name, using a dynamically created form and submitting it using JavaScript. We're creating two hidden input fields in the form to hold the values of the parameters. We then set the action attribute of the form to the URL of the PHP file and submit the form.
In the PHP file, we're accessing the values of the id and name parameters using the $_POST superglobal variable and displaying them using the echo statement.
You can modify the example to pass different parameters and access them in your PHP file as needed.
Comentarios
Publicar un comentario