PHP sends back empty body - php

I'm trying to send form data from Vue Resource to my PHP page. I can see it displayed on the PHP page but when I send it back as JSON I get an empty response. Why is it sending an empty response?
Edit:
It looks like the problem is that the value for the submit button is not set in PHP. I am not sure why that is happening. Tried using $_REQUEST and axios/$.post but it makes no difference.
PHP:
if(isset($_POST['submit']){
echo json_encode($_POST);
}
JS:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
.then(data => console.log(data));
HTML:
<form class="col s12" id="submitalbum" method="post" action="addalbum.php">
<div class="row">
<div class="input-field col s6">
<input name="artist" placeholder="Artist" type="text">
</div>
<div class="input-field col s6">
<input name="title" placeholder="Title" type="text">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="genre" placeholder="Genre">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="released" type="number" name="released" placeholder="Year Released">
</div>
<button #click.prevent="addNewAlbum" name="submit" class="waves-effect waves-light btn">Submit</button>
</div>
</form>

The empty response is returned for multiple reasons. See them explained below.
jQuery selector
It appears that the selector for finding the form uses the jQuery id selector:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
But the jQuery function (i.e. $()) returns a collection (an array):
var submitAlbumCollection = $('#submitalbum');
console.log('submitAlbumCollection: ', submitAlbumCollection);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="col s12" id="submitalbum" method="post" action="addalbum.php"></form>
However FormData() needs "An HTML <form> element"1, not an array.
So if you are going to use the jQuery function, take the first element from it to get the reference to the form:
var forms = $('#submitalbum');
if (forms.length) {
this.$http.post('addalbum.php', new FormData(forms[0]))
button elements don't send value
A <button> is not a form input and hence there would not be a corresponding value in the form data. This if you want to check for form values, try checking for the artist, title, etc.
if(isset($_POST['artist'])) {
echo json_encode($_POST);
}
If you really wanted to check if $_POST['submit'] is truthy, you could:
add a hidden input:
<input type="hidden" name="submit" value="1">
convert the button to a submit button
<input type="submit" name="submit" value="1">
take the suggestion from this answer and manually append an entry to the form data
See a demonstration in this phpfiddle. Note that phpfiddle doesn't allow multiple pages so the code from addalbum.php was placed at the top, and references to it were replaced with <?php echo $_SERVER['PHP_SELF'];?> (i.e. in the client-side code).
Missing parenthesis
Additionally, there is a missing parenthesis on the first line of the PHP code:
if(isset($_POST['submit']){
To correct this, add the missing closing parenthesis to close the expression:
if(isset($_POST['submit'])){
1https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

Related

How to send the user to different pages when the checkbox is checked?

<form entype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
Hi hello I want to ask a simple question. The code above is search.php,
I want to send the form to a different page based on if the checkbox is checked or not. If the checkbox is checked it will be directed to resqueryexp.php, but if not it will be directed to result.php
I have been trying to adding this code but it doesn't work.
<?php
if (isset($_GET['queryexp'])){
header("Location: resqueryexp.php");
}else{
header("Location: result.php");
}?>
Sorry for my bad English and Thanks in advance.
<?php
if ( isset( $_GET['submit'] )) {
if ($_GET['queryexp'] == 1 ){
header("Location: resqueryexp.php");
exit;
}
else
{
header("Location: result.php");
exit;
}
}
?>
<html>
<head><title>test</title>
</head>
<body>
<form enctype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
This code won't run here at SO, but this is how it may work on your webserver. The important part is to test if the form was submitted. So, in this case, I gave the submit button the name of "submit" and then tested with PHP to see if the form was even submitted. If the form is submitted and if the checkbox is checked, the redirect via header() occurs. Otherwise, if the checkbox is unchecked, then the redirect occurs via header to result.php. You may avoid header issues by making an adjustment to you PHP.ini settings and adding this line "output_buffering = On".
Note: usually a form with the enctype attribute having a value of "multipart/form-data" involves submitting a file and under such circumstances the method attribute should be a POST request instead of a GET; see MDN.
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
http://php.net/manual/en/function.header.php

Submitting a form without action or method or any other javascript code

A friend asked me add pattern code to prevent typing email ids and phone numbers, on his WP template site.
As i have added the pattern code to the input field, it still accepts email ids and numbers.
So, i figured that may be the form submit php code got some changes needed. but trickily his form tag doesn't have any action tag,. it just have form ID.
I don't know what to do. I just need to make the pattern work in the input field to prevent email id's and numbers.
the code goes as:
<div class="col-xs-12 col-sm-8 col-md-9">
<div class="message-entry">
<form id="message_reply_form">
<div class="form-group" >
<input type="text" autocomplete="off" class="form-control mb-10" placeholder="<?php esc_html_e( 'Your Message', 'extretion' ); ?>" name="reply_message" pattern="^(?!.*([a-zA-Z0-9._%+-]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)).(?:\b\d{10,11}\b)[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" >
</div>
<input type="submit" class="btn btn-primary btn-sm reply_message_btn" value="<?php esc_html_e( 'Reply', 'extretion' ); ?>">
</form>
</div>
</div>
Any help is appreciated..
network screenshot at the time of message submit
In HTML5, the action attribute is no longer required.
source
if I remember correctly, it defaults to current page. This is why the form is still submitted.
Have you tried giving a action attribute to your form?
<form action='your/url'>
The body of your form
</form>

Create session variable from html form input data

I have a form on page a.php with php code which should set the data from the form into php session variables, however i am having trouble making it work. I have session_start(); at the beginning of every page i want to do this, but its at the top of the html so that is why its not in this piece.
Here is the code:
<form class="form1" method="post" action="" id="form1">
<div class="form-group add_to_cart_prompt">
<span class="">Add something to cart</span>
</div>
<div class="form-group">
<input type="text" name="sticker" class="form_sticker_name" value="something">
<label class="quantity_desc" for="quantity" title="how much?">Quantity</label>
<input class="btn btn-default quantity_input" type="number" id="quantity" name="quantity" placeholder="how much ?" min="0" required>
</div>
<div class="form-group bottom_buttons">
<button type="submit" name="submit" class="btn btn-default add_yes">Add to cart</button>
<button type="reset" class="btn btn-default">Clear</button>
<button type="button" class="btn btn-default add_no">Close</button>
</div>
</form>
PHP which is executing the form is on the same page as the form:
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['sticker'])) {
$sticker_name = $_POST['sticker'];
$_SESSION['sess_sticker'] = $sticker_name;
}
}
?>
I can set session variables to some string, and that works ok, but when i try to put data from the form it gives me this error:
Notice: Undefined index: sticker in
B:\Programs\xampp\htdocs\D2S\shopping_cart.php on line 99
I have looked up how to fix the error, and have been trying a lot and can't fix it. Thank you for trying to help.
In PHP, a variable or array element which has never been set is different from one whose value is null; attempting to access such an unset value is a runtime error.
That's what you're running into: the array $_POST does not have any element at the index "sticker", so the interpreter aborts your program before it ever gets to the nullity test.
You can test for the existence of a variable or array element without actually trying to access it; that's what the special operator isset does:
if (isset($_POST['sticker'])) {
//do something
}
Also I have tested your form code on my machine, and it seemed to be working fine and session variable was set using form data.

codeigniter - form returning no data

I'm trying to create a page with a form using ci.
When i submit the form, the controller says that I have no data that's been submitted.
I can't see where my error lies.
Here's the view:
<?php echo validation_errors(); ?>
<?php echo form_open('widgets/search/'.$hardwaremodel.'/'.$objectid.'/'.$name.'/'.$fd); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="searchstring" placeholder="last 4 characters" size="4">
</div>
<button type="submit" class="btn btn-default">Search</button>
<button type="cancel" class="btn btn-default">Cancel</button>
</form>
Once the page renders, the form tag ends up looking like this:
<form action="http://myserver/myciapp/index.php/widgets/search/205406zl/5461/SW-1/SW1net" method="post" accept-charset="utf-8">
The controller:
public function search()
{
$searchstring = $this->input->post(); // form data
var_dump($searchstring);
exit;
}
The results of the var_dump shows:
bool(false)
Thanks
EDIT 1
I haven't posted the entire HTML page that includes the form... but I display some of the fields passed in the URI as headings on the page - just before I create the form. Hope that clarifies...
Would this impact the POST data? Why is that relevant?
Thanks
A few things I'd suggest doing. First is, if you are going to include other variables in the form_open tag, I would add those variables to your controller, and put them in the form_open tag as URI strings. This will allow the form validation to work if you are going to echo out validation errors.
Also, you should be calling a name on the input->post() to get the specific item, (but you don't need to to get all POST data).
Controller:
public function search($hardwaremodel, $objectid, $name, $fd) {
$searchstring = $this->input->post('search_string'); // form data
var_dump($searchstring);
exit;
}
View:
<?php echo form_open('widgets/search/'.$this->uri->segment(3).'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6)); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="search string" name="search_string" placeholder="last 4 characters" size="4">
</div>
Form elements are referenced by name attribute which is missiong on input field searchstring.
Add:
name="searchstring"
on your input field.
in this code you have not used name attribute.You use id.
try this one
<input type="text" name="searchstring" value="xyz">

submit form to page and depending on input show different div

I Have a form which when submitted needs to go to the page and then show one of 4 hidden divs depending on the page.
Here is the form
<form>
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Here is the page
<div id="brighton">
<p>Brighton</p>
</div>
<div id="devon">
<p>Devon</p>
</div>
<div id="search">
<p>search</p>
</div>
<div id="variety">
<p>variety</p>
</div>
So if Brighton is typed into the place input i need the form to submit the page and show the Brighton div and if Devon is typed in to show the Devon div etc and if the 2/12/2012 is typed into the date picker input and Brighton into the place input it goes to the page and shows the variety div.
i also need it so if the 1/12/2012 is typed in to the date picker input the page redirects to the page show.html.
any help would be greatly appreciated
thanks.
This is easy if you know PHP at all. It looks like you need a good, easy start. Then you will be able to achieve this in seconds.
Refer to W3SCHOOLS PHP Tutorial.
To achieve what you have mentioned, first make the following changes in your form:
<form action="submit.php" method="post">
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Create a new file called submit.php and add the following code:
<?php
$place = $_POST['place'];
$date = $_POST['datepicker'];
if ($date == '1/12/2012') {
header('Location: show.html');
exit;
}
?>
<?php if ($place == 'Brighton''): ?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php elseif ($place == 'Devon'): ?>
<div id="devon">
<p>Devon</p>
</div>
<?php elseif ($place == 'search'): ?>
<div id="search">
<p>search</p>
</div>
<?php elseif ($place == 'Variety'): ?>
<div id="variety">
<p>variety</p>
</div>
<?php endif; ?>
Now the above example is not the complete solution, but it gives you an idea as to how you can use if-then-else construct in PHP to compare values and do as desired.
Post your form to a php page and then check the posted form parameters to determine which div to show.
<?php
if ($_POST["place"] == "Brighton") {
?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php
} else if ($_POST["place"] == "Devon") {
?>
<div id="devon">
<p>Devon</p>
</div>
<?php
}
?>
Do that for each div and parameter combination. Make sure you set the "method" attribute on your form to "post":
<form action="somepage.php" method="post">...</form>
In the resulting HTML you will only see the one that matches the form parameter.

Categories