Symfony get single form field post value in controller method - php

I'm using symfony 2.8, I'm submitting a registration from to registerAction method. I'm getting all my submitted data like this when I use $form-getdata() in registerAction method.
AppBundle\Entity\User Object
(
[id:AppBundle\Entity\User:private] =>
[name:AppBundle\Entity\User:private] => testname
[email:AppBundle\Entity\User:private] => test#test.com
[roles:AppBundle\Entity\User:private] => Admin User
[plainPassword:AppBundle\Entity\User:private] => 123456
[password:AppBundle\Entity\User:private] =>
)
I have used $request->request->get('name') to get single name value but it is showing blank screen and printing nothing.
My html form looks like this on viewing source code in browser.
<form name="user" method="post" novalidate="novalidate">
<div class="form-group">
<div><label for="user_name" class="required">Name</label><input type="text" id="user_name" name="user[name]" required="required" class="form-control" /></div>
</div>
<div class="form-group">
<div><label for="user_email" class="required">Email</label><input type="email" id="user_email" name="user[email]" required="required" class="form-control" /></div>
</div>
<div class="form-group">
<div><label for="user_roles" class="required">Roles</label><select id="user_roles" name="user[roles]" class="form-control" style="margin:5px 0;"><option value="Normal User">Normal User</option><option value="Admin User">Admin User</option></select></div>
</div>
<div class="form-group">
<div><label for="user_plainPassword_first" class="required">Password</label><input type="password" id="user_plainPassword_first" name="user[plainPassword][first]" required="required" class="form-control" /></div>
</div>
<div class="form-group">
<div><label for="user_plainPassword_second" class="required">Repeat Password</label><input type="password" id="user_plainPassword_second" name="user[plainPassword][second]" required="required" class="form-control" /></div>
</div>
<div><button type="submit" id="user_Register" name="user[Register]" class="btn btn-success form-control">Register</button></div><input type="hidden" id="user__token" name="user[_token]" value="epZFHVctxHFlsnxG-H4OdS92DmGVjP1DDF-bKJrV2Ss" />
</form>
How can I get single posted form value.?

If $request->request->get('{form_name}') returns an array with your form data, then you can access the specific input field with $request->request->get('user')['name']. But you should use form classes instead, where you can define all of necessary fields with validation constraints, etc. Hope this helps!

Related

AMP Project doesn´t excecute the PHP code as Form or even as echo

I have a website AMP project and I wanted to use PHP for forms.
On the webpage form which has a post method returns no action in the backend code.
So I think that the data inn´t transfered.
edit on answer:
My form code is:
<form method="POST" class="sit-form" action-xhr="ampsend.php" data-form-title="contactForm">
<div class="sit-row">
<div submit-success="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">success!
</template>
</div>
<div submit-error="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">failed! {{error}}</template>
</div>
</div>
<div class="dragArea sit-row">
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="firstnameHidden" value="Firstname"
id="firstnameHidden"
data-form-field="">
<input type="text" name="firstname" placeholder="Firstname" data-form-field="Name"
required="required" class="field-input display-4" value=""
id="firstname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="lastnameHidden" value="Lastname"
id="lastnameHidden"
data-form-field="">
<input type="text" name="lastname" placeholder="Lastname" data-form-field="Name"
class="field-input display-4" required="required" value=""
id="lastname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="emailHidden" value="Email" id="emailHidden"
data-form-field="">
<input type="email" name="email" placeholder="Email" data-form-field="Email"
required="required" class="field-input display-4" value=""
id="email">
</div>
<div class="sit-col-md-12 sit-col-sm-12 field">
<input type="hidden" name="messageHidden" value="Message"
id="messageHidden"
data-form-field="">
<textarea name="message" placeholder="Message" data-form-field="Message"
class="field-input display-4" required="required" value=""
id="message"></textarea>
</div>
<div data-for=""
class="sit-col-md-12 sit-section-btn sit-pt-4 align-center sit-col-sm-12 field">
<button type="submit" class="btn btn-primary display-4">
send
</button>
</div>
</div>
</form>
The file_put_contents of the POST by clicking on submit is:
a:8:{
s:15:"firstnameHidden";
s:9:"Firstname";
s:9:"firstname";
s:8:"TestName";
s:14:"lastnameHidden";
s:8:"Lastname";
s:8:"lastname";
s:12:"TestLastname";
s:11:"emailHidden";
s:5:"Email";
s:5:"email";
s:13:"test#test.com";
s:13:"messageHidden";
s:7:"Message";
s:7:"message";
s:11:"testMessage";}
So does it work correctly? because as return I get the "failed!" - message from the AMP template.
I think you cannot execute PHP code on the displaying document. PHP is serving the data to show through an POST on the displaying web document as described in this post.
My solution to check if PHP is running ,and check what data was posted via form - was write everything to text file.
$serializedData = serialize($_POST);
file_put_contents('your_file_name.txt', $serializedData);
Open file and see what data was pass. If you want display message after form was submitted and pass some date like name or email. You need to use amp-mustache. And and return JSON form PHP to pass it back to your AMP page.
For example in your PHP file:
header('Content-Type: application/json');
echo json_encode(array( 'name' => $_POST['name]' ));
Of course don't forget to put appropriate CORS headers in PHP or you .htaccess file.

Data from JSON response not displaying after assigning to instance variable

So, I have my SPA at about 98% functional. The app pulls data from a MySQL database and allows the user to edit/delete records. For the page in question, it will edit/delete data of the specified student ID but it will not properly display the data in the text fields.
If you do not input a value it will display the first item in the JSON array but not the one you specify in the search field.
I did not do a very good job at explaining that but here is the HTML page that uses a function to pass data to the controller which then selects the corresponding student by ID and assigns it to the variable $scope.student. I then try to display the student data on the HTML page by using student.first_name (or any property) but it does not work correctly.
<h3>Edit/Delete Student with ID: {{student.student_id}}</h3>
<div class="form-group">
<label for="sid">Student ID:</label>
<input type="text" class="form-control" id="sid" ng-model="sid">
</div>
<p><button type="button" class="btn btn-primary" ng-click="getRecord(sid)">
Get Student Info </button> </p>
<div class='row'>
<div class="col-md-6">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" id="first_name" ng-model="student.first_name">
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" id="last_name" ng-model="student.last_name">
</div>
<div class="form-group">
<label for="hrs_completed">Hrs Completed:</label>
<input type="text" class="form-control" id="hrs_completed" ng-model= "student.hrs_completed">
</div>
<div class="form-group">
<label for="hrs_attempted">Hrs Attempted:</label>
<input type="text" class="form-control" id="hrs_attempted" ng-model= "student.hrs_attempted">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="gpa_points">GPA Points:</label>
<input type="text" class="form-control" id="gpa_points" ng-model= "student.gpa_points">
</div>
<div class="form-group">
<label for="major">Major:</label>
<input type="text" class="form-control" id="major" ng-model="student.major">
</div>
<div class="form-group">
<label for="advisor_id">Advisor ID:</label>
<input type="text" class="form-control" id="advisor_id" ng-model="student.advisor_id">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" ng-model="student.email">
</div>
</div>
<button type="button" class="btn btn-primary" ng-click="updateRecord()">Update</button>
<button type="button" class="btn btn-primary" ng-click="deleteRecord()">Delete</button>
And here is my controller on my Javascript page:
app.controller('editCtrl', function($scope, $http) {
$http.get("getStudentData.php")
.then(function (response) {
$scope.students = response.data;
});
$scope.getRecord = function(sid) {
id = sid;
$scope.student = $scope.students.find(s=>s.id == sid);
};
Do I need to make a seperate GET request to the server for this to work properly or do I just need to reference the student object differently?
I think there is mismatch in what you are getting as response.data and what you are trying to .find() and then what you are binding on html.
Check this plunkr.
You are trying to render by searching id property.
$scope.students.find(s=>s.id == sid);
where as you are rendering on UI using student_id property.
{{student.student_id}}
So, surely there is mismatch in what you are getting as response.data and what you are rendering using $scope.student properties. I think my plunkr will show that your function is correct.
In case this answer doesn't work, share your response.data.

Submit form with php WordPress

Hello I work with WordPress , and I create a custom Modal with form to add a Group called 'abonnement' I added the table in Database and this is my issue:
Form
<form name="abonnementCreate" action="create.php" method="POST">
<div class="form-group">
<label>Nom de l'abonnemnt</label>
<input type="text" class="form-control" id="nomA" name="nomA">
</div>
<div class="form-group">
<label>Durée</label>
<input type="number" class="form-control" id="dureeA">
<small id="dureeHelp" class="form-text text-muted">En jours</small>
</div>
<div class="form-group">
<label>Nombre de produit autorisé</label>
<input type="number" class="form-control" id="nbProdA">
</div>
<div class="form-group">
<label>Frais d'abonnement (€)</label>
<input type="number" class="form-control" id="fraisA">
</div>
<button type="submit" name="create" class="btn btn-primary">Valider</button>
</form>
and this is the action of the creation :
create.php
<?php
include'admin.php';
global $wpdb;
if(isset( $_POST["create"]))
{
$wpdb->insert('abonnement', array(
'nom' => '.$_POST["nomA"].',
'duree' => '.$_POST["dureeA"].',
'nbProd' => '.$_POST["nbProdA"].',
'frais' => '.$_POST["fraisA"].'
));
}
?>
I have $_POST["create"] and the condition is true but $_POST[" any field"] is always null,
Someone please can help me ?
you can never catch the post value from id of the form input field.only way that you can get value using $_POST[] is using the "name" attribute of the input field.
<input type="text" class="form-control" id="nomA" name="name1">
this input field value can be accessed from form action php file using $_POST["name1"]

PHP not getting any response from the form on the previous page

I have the file "1.php" and the file "2.php" ...
In "1.php", there's a HTML form:
<form action="2.php" method="post">
<div class="form-group">
<label for="db_username">Field 1:</label>
<input type="text" class="form-control" id="db_username">
</div>
<div class="form-group">
<label for="db_password">Field 2:</label>
<input type="password" class="form-control" id="db_password">
</div>
<div class="form-group">
<label for="db_passphrase">Field 3:</label>
<input type="text" class="form-control" id="db_passphrase">
</div>
<button type="submit" class="btn btn-default">Next</button>
</form>
While in "2.php", the action must be applied in PHP .. like that:
<?php
// Process of Step "1"
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_passphrase = $_POST['db_passphrase'];
if( !isset($db_username) || !isset($db_password) || !isset($db_passphrase) ) {
header("Location: 1.php?error=1");
die();
}
?>
However .. the values of $_POST['db_username'], $_POST['db_password'] and $_POST['db_passphrase'] is empty...
You need to use the name attribute to later access the $_POST data, as in:
<input type="text" class="form-control" id="db_username" name="db_username">
You need to give the inputs a name and acces that value through $_POST['name'].
id is not relevant for php form handling
You have to give name for the textfields like this
<input type="text" class="form-control" id="db_username" name="db_username">
Put name instead of id. it should work

Access HTML form data in Symfony2

I am new to Symfony. I have seen a lot of threads regarding this topic, but none have been able to answer the question that I have.
I have a "Contact" form on my site. This contact form submits to path('submit_query'), which calls the submitQueryController. I am not building the form through Symfony and I am not using an object or entity. My problem is that no matter what I do, I am unable to access the form data within the controller. I have tried every thread suggestion that I have seen, and I either get the REQUEST object with a whole bunch of data (none of which is my form data) or I get nothing.
Is there no easy way of accessing the posted form data from within the controller?
My HTML Form:
<form id="contact_form" role="form" action="{{ path('submit_query') }}" method="post">`
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input type="text" class="form-control" id="name" placeholder="Full Name" data-validation-error-msg="Please enter your full name" data-validation="length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="email" class="control-label">Email Address</label>
<input type="email" class="col-sm-3 form-control" id="email" placeholder="Email Address" data-validation-error-msg="Please enter a valid email address" data-validation="email length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="number" class="control-label">Contact Number</label>
<input type="text" class="form-control" id="number" placeholder="Contact Telephone Number">
</div>
<div class="form-group">
<label for="subject" class="control-label">Subject</label>
<input type="text" class="form-control" id="subject" placeholder="The subject of your query" data-validation-error-msg="Please enter a subject for your query" data-validation="length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="query" class="control-label">Query</label>
<textarea class="form-control" id="query" rows="5" placeholder="Please enter a detailed description of your query" data-validation-error-msg="Please enter your query description" data-validation="length" data-validation-length="min1"></textarea>
</div>
</fieldset>
</div>
<div class="panel-footer clearfix text-center"><button type="submit" class="btn btn-default">Submit Query</button></div>
</form>
Controller:
When I try:
public function submitQueryAction(Request $request)
{
$data = $request->request->all();
die(var_dump($data));
}
I get an empty array in "$data".
When I try:
public function submitQueryAction()
{
$data = $this->getRequest()->request->all();
die(var_dump($data));
}
I get a vardump of a Request object, but none of the data is mine. I have also tried the solution presented by Access POST values in Symfony2 request object for getting post values without using an object or entity, but I get an error "Call to undefined method Symfony\Component\Form\Form::bindRequest()".
PLEASE HELP.
Thank you in advance.
You are missing the name html attribute for your inputs.
If an element of the form misses this attribute, then its data will not be sent.
From w3.org :
Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

Categories