enviar un objeto php desde un archivo php con html a otro archivo php
Frecuentes
Visto 71 veces
0
profiles_info.php
I have a php document that receives information from Ajax. I then set that information as attributes to an object of class Profile. I have html that displays a table. When the user clicks on Add the object should be sent to add_to_database.php where I want to access all of the objects attributes.
$firstName = $_POST['fn'];
$lastName = $_POST['ln'];
class Profile{
public $firstName;
public $lastName;
public function setFirstName($fn){
$this->firstName = $fn;
}
public function setLastName($ln){
$this->lastName = $ln;
}
public function getFirstName(){
return $this->firstName;
}
public function getLastName(){
return $this->lastName;
}
}
$person = new Profile();
$person->setFirstName($firstName);
$person->setLastName($lastName);
echo "<p><table border= \"1\"><tr><th> First Name:</th><th> Last Name:</th><th> Add To Database:</th></tr><tr><td>" . $person->getFirstName() . "</td><td>" . $person->getLastName() . "</td><td><a href='add_to_database.php'>Add</a></td></tr></table></p> ";
How do I send the firstname and lastname to add_to_database.php?
I have tried include, but the html appears. I have also tried sessions by storing the $person object in a session but I don't know how to access getFirstName and getLastName in add_to_database.php. The $person object will have more attributes later so i don't want to store every single attribute in a session. Is there any other way to do this?
1 Respuestas
1
What i suggest is to serialize
your object and put it in a session variable ,and in your seconde page unserialize
it and call your get methodes.
Ejemplo:
$person = new Profile();
$s = serialize($person);
$_SESSION['mySerializedObject']=$s;
And in your seconde page :
include 'Profile Class file here';
$u = unserialize($_SESSION['mySerializedObject']);
echo $u->getFirstname();
Respondido el 09 de Septiembre de 13 a las 21:09
I have tried your suggestion, but if i use include my html appears on add_to_database.php also it gives out errors since it does not recognize the POST objects in profiles_info.php. $person->getFirstName() is not retrieving anything. - user2687915
it's a better to put you class definition in a separate file - Charaf JRA
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php html ajax or haz tu propia pregunta.
Share your object using sessions as you said , and use your getters like in your local file , but , include you class file and cast it - Charaf JRA
So add some database code. Since you haven't shown any of this
add_to_database.php
script, we can't help you. None of the code you've shown even REMOTELY involves a database. - Marc B@MarcB He just wants to get the call to that file. He's not asking how to code once it's there. - Cruncher
You can AJAX betweem php files. You can actually just send it directly to add_to_database.php from wherever your other ajax call is - Cruncher
How can i also send it to add_to_database.php at the same time. I'm new to AJAX. - user2687915