the_summer_bee
the_summer_bee

Reputation: 493

pass string using jquery ajax to php

I want to send a hash string to my php file. but failed, can anyone help me please?

my javascript is like this:

var hashString = "#support_form";

$.ajax ({
   type: "POST",
   url:"index.php",
   data: hashString,
   success: function() {
      console.log("message sent!");
   }
});

and the php:

<?php
$theHash = $_POST['hashString'];
?>

What should I do? Thanks

Upvotes: 2

Views: 13542

Answers (8)

Mikul
Mikul

Reputation: 1

var queryString = "f_name=" + f_name + "&l_name=" + l_name + "&contact_no=" + contact_no + "&email=" + email;
$.ajax({
    type:'POST',
    url:'insert1.php',
    data:queryString,
    dataType:'json',
    success: function(success){
console.log("message sent!");
}
});

Upvotes: 0

Renjith
Renjith

Reputation: 3274

$.ajax ({
   type: "POST",
   url:"index.php",
   data: {"hashString":hashString},
   success: function() {
      console.log("message sent!");
   }
});

Upvotes: 0

Sudhanshu Yadav
Sudhanshu Yadav

Reputation: 2333

data key must be of object type This will work for you

var hashString = "#support_form";

$.ajax ({
   type: "POST",
   url:"index.php",
   data: {'hashString':hashString},
   success: function() {
      console.log("message sent!");
   }
});

Upvotes: 0

Kyle
Kyle

Reputation: 1733

Your JS should be this:

var hashString = "#support_form";

$.ajax ({
   type: "POST",
   url:"index.php",
   data: {hashString: hashString},
   success: function() {
      console.log("message sent!");
   }
});

Upvotes: 0

Brian Glaz
Brian Glaz

Reputation: 15676

Your code would work if you used an object instead of a string. try this:

var hashString = {hashString: "#support_form"};

$.ajax ({
   type: "POST",
   url:"index.php",
   data: hashString,
   success: function() {
      console.log("message sent!");
   }
});

Upvotes: 0

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

You have to do like this-

$.ajax ({
   type: "POST",
   url:"index.php",
   data: "hashString=value",
   success: function() {
      console.log("message sent!");
   }
});

So you can get the value as-

$theHash = $_POST['hashString'];
echo $theHase; //will print value

Upvotes: 2

wirey00
wirey00

Reputation: 33661

You need to specify the name/value for data

data: {hashString:hashString},

Upvotes: 5

Tom Walters
Tom Walters

Reputation: 15616

Use:

data: 'hashString=' + encodeURIComponent(hashString),

Upvotes: 0

Related Questions