The check does not work:- if (isset ($_PHP["form_name"])) [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
There are several forms for queries in the file, you need to do a form check, if you do not do this, all forms with queries are executed, as for me it is not correct. Here with the help of if (isset($_POST ['form_name'])) I do a check, but does not exit. What is the problem?
<form id="form" method="POST">
<input type="text" name="telephone[]">
<input type="text" name="telephone[]">
<input type="hidden" name="form_name" value="form_ex">
<button type="submit" name="update" id="btn_post_phones" value="button_value">Update phone</button>
</form>
if (isset($_POST['form_name'])) {
var_dump ('true -->');
echo "<pre>";
var_dump(json_decode($_POST["phones"], true));
} else {
var_dump ('false-->');
echo "<pre>";
var_dump(json_decode($_POST["phones"], true));
}
//string(8) "false-->"
//array(2) {
// [0]=>
// string(6) "345345"
// [1]=>
// string(6) "345345"
//}
$('#form').submit(function(event) {
var arrPhones = new Array();
event.preventDefault();
$('input[name="telephone[]"]').each(
function() {
arrPhones.push($(this).val());
});
var data='phones='+JSON.stringify(arrPhones);
$.ajax({
type: 'POST',
url: 'regist.php',
dataType: 'json',
data: data,
// data: $('input[name="telephone[]"]').serialize,
beforeSend: function(){
console.log('before ' + data);
},
success: function(response){
console.log(response);
}
});
});

1.Remove var data='phones='+JSON.stringify(arrPhones);
2.instead of data: data, write data:{'phones':JSON.stringify(arrPhones)},
3.Instead of if (isset($_POST['form_name'])) {...} use if (isset($_POST['phones'])) {...}

Related

Why the javascript code doesn't connect to the php file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
Javascript code
$(document).ready(function () {
//Add to cart function
$('.add_to_cart').click(function (e) {
//alert("add to cart");
e.preventDefault();
var product_id = $(this).val();
$.ajax({
method: "POST",
url: "functions/carthandler.php",
data: {
"product_id": product_id,
"scope": "add"
},
dataType: "dataType",
success: function (response) {
alert("success");
if (response == 401) {
alert("Login to continue");
}
}
});
});
});
PHP code with path 'functions/carthandler.php'
<?php
include('../includes/connect.php');
session_start();
if (isset($_SESSION['username'])) {
if (isset($_POST['scope'])) {
$scope = $_POST['scope'];
echo "<script>alert('success')</script>";
exit;
}
} else {
echo 401;
}
?>
I've checked whether js code connect to the php code using echo, but the php doesn't seem to have respond anything. I've also tried rechecking the indentation and everything and still not working.

What is wrong js and php objects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In test.php i have this code
<?php
if (isset($_POST['user'])) {
echo "hi";
}
exit; ?>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
//do whatever.
}
});
</script>
</head>
</html>
I changed it, but isset($_POST['user']) = false because my page is empty so why?
All this code is in test.php
You are not printing / visualizing the actual response of the ajax.
<?php
if (isset($_POST['user'])) {
$user_string = $_POST['user'];
var_dump($user_string);
$user_asarr = json_decode($user_string, true); // associative array
var_dump($user_asarr);
exit();
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
console.log(response); // check out your console!
}
});
</script>

Getting JSON data with jquery ajax not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to understand how to fetch and display data with jquery ajax. I have a php page (data.php)that successfully retrieves data from a mysql database and encodes that data into a json array. Client side I have a page called get.php. I just can't figure out why my script will not fetch any data from data.php I get nothing in the firebug console.
data.php
echo json_encode($mydata);
which outputs:
[
{
"id":"236",
"title":"The Jungle Book"
},
{
"id":"235",
"title":"The Shallows"
},
{
"id":"232",
"title":"For Your Eyes Only"
},
{
"id":"231",
"title":"Ice Giants"
}
]
get.php
<script>
("button").click(function(){
{
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var title = data[1];
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
}
});
});
</script>
<h3>Output: </h3>
<button>Get Data</button>
<div id="output"></div>
You have few mistake like: you didn't specify jquery($) for button
selector, you use multiple bracket { inside click function, inside
ajax success you have assigned full object against id and title it
should be id=data[0]['id'] and title=data[0]['title] and another
mistake there no defined variable vname. php better json output you should use header('Content-Type: application/json'); in data.php.
Try this:
index.php
<h3>Output: </h3>
<button>Get Data</button>
<div id="output"></div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
$("button").click(function(){
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data){
//console.log(data);
var id = data[0].id;
var title = data[1].title;
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+title);
}
});
});
</script>
data.php
<?php
header('Content-Type: application/json'); //use header to specify data type
//echo json_encode($mydata); // un-comment this line
echo '[{"id":"236", "title":"The Jungle Book"}, {"id":"235", "title":"The Shallows"}, {"id":"232", "title":"For Your Eyes Only"}, {"id":"231", "title":"Ice Giants"} ]'; // comment this line
?>
<script>
$("button").click(function()
{
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var title = data[1];
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
}
});
});
try like this

extract json data in php and print it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
array(2) {
["data"]=> array(1)
{
["MemberData"]=> array(1)
{
["Verified"]=> string(1) "Y"
}
}
["error"]=> array(2)
{
["code"]=> string(0) ""
["text"]=> string(0) ""
}
}
/i want to print verified value in a php page/
This is the way to do it from PHP to javascript:
var ar = JSON.parse( '<?php echo json_encode($array) ?>' );
//ar[1]['MemberData']; //first memberdata value
console.log(ar[1]['MemberData'].value);
and from javascript to php you need to use ajax
jQuery.ajax({
type: "POST",
url: 'xxxxxxx',
dataType: 'json',
data: {array:array, //here you can pass the verified value of the array or the whole array if you want
},
success: function(data) {
if(data.success == true){
}
},
error: function(data) {
if(data.success != true){
}
},
});
And then you can read it like normal POST on PHP-side.
Otherway of doing it would be to reload the URL with ?verified='javascript_array_value' and read it with GET

Submitting form with jQuery

Apologies if this has been answered before (I couldn't find the answer when I searched the archives)
I've got a page protected by a password:
<?php
if($_POST['pw'] == 'pw')
{
//Page content
} else
{
//Display password form
}
?>
Within the page content, I've got another form, which I want to submit using jQuery, and have the following code:
<script type='text/javascript'>
var dataString = $('input#input1').val();
$(function() {
$('#submit').click(function()
{
$.ajax({
type: 'POST',
url: 'p2.php',
data: dataString,
dataType: html,
success: function(data2) {
$('#testResult').html(data2);
}
});
return false;
});
});
</script>
<form name='form1' id='form1' action=''>
<fieldset>
<label for='input1' id='input1_label'>Input 1</label>
<input type='text' name='input1' id='input1' size='30' />
<input type='submit' value='Update / reset' id='submit' class='buttons' />
</fieldset>
</form>
<div id='#testResult'></div>;
However, clicking submit then sends the form to p1.php?input1=test (i.e., the data string is being sent to p1.php, not p2.php). If I edit the code and remove dataType:html and the 2 references of data2, then this doesn't happen (infact, nothing happens, so I assume that jQuery is submitting the data to the form). I've also changed the type to 'GET', incase the 2 POST requests on the same page were causing problems, but this didn't change the result.
What am I missing to get the information from p2.php (i.e. data2) and displaying it?!
EDIT
Thanks to a comment pointing out a typo, I've changed dataType: html to dataType: 'html' - this now doesn't cause the page to redirect to p1.php?input1=test, but once again, it doesn't do anything (when it should still be returning the value of data2)
EDIT 2
I've updated the code so dataString is now:
var dataString = $('input#input1').val();
dataString = 'var1='+dataString;
but this hasn't made any difference
For clarification, my p2.php just contains the following:
<?php
echo "<p>HELLO!</p>";
?>
EDIT 3
I made the changes to my code has suggested by Damien below; I get the alert of "works!" but still nothing seems to be returned from p2.php, and nothing is inserted into the #testResult div.
var dataString = $('input#input1').val();
$(function() {
$('#submit').click(function(evt)
{
evt.preventDefault();
$.ajax({
type: 'POST',
url: 'p2.php',
data: "someval="+dataString,
dataType: 'html',
success: function(data2) {
$('#testResult').html(data2);
}
});
return false;
});
});
$(function() {
$('#submit').click(function()
{
var dataString = $('#form1').serialize();
$.ajax({
type: 'POST',
url: 'p2.php',
data: dataString,
success: function(data2) {
alert('works!'); // ADDED AFTER UPDATE
$('#testResult').html(data2);
},
/* ADDED AFTER UPDATE */
error:function(obj,status,error)
{
alert(error);
}
});
return false;
});
});
Edit:
In p2.php:
<?php
var_dump($_POST['pw']);
?>
In p2.php you then need to output ( using echo, for example) what you want to be returned as 'data2' in your ajax success call.
UPDATE:
Since you're Ajax request fires succesfully, that means either your post is not passed correctly, or you're not outputting anything. I've re-looked at your code and I saw this:
<input type='text' name='input1' id='input1' size='30' />
that means you're fetching the wrong $_POST variable!
Do this:
Since you're sending a name="input1", in your p2.php try with:
<?php
if(isset($_POST['input1'])
{
echo $_POST['input1'];
}
else
{
echo 'No post variable!';
}
And in your jquery success:
success: function(data2) {
alert(data2);
$('#testResult').html(data2);
},
That oughta work, if you follow it literally. In the remote possibility it won't work, forget AJAX, remove the javascript and do a normal post submitting with p2.php as an action of your form :)
I think you have to prevent the default action of the form.
Try this:
$('#submit').click(function(e)
{
e.preventDefault();
$.ajax({
type: 'POST',
url: 'p2.php',
data: dataString,
dataType: html,
success: function(data2) {
$('#testResult').html(data2);
}
});
return false;
});
});
The data should be formatted like this:
variable1=value1&variable2=varlue2
I also think you can remove the dataType property.

Categories