0

I have a strange behaviour, I sent data to a PHP page using Post request, my problem is when I try to manipulate this data (echo for exemple) in PHP page I get nothing, but when I make condition on the value I sent the condition is ok.

My JS code

$.ajax({
    type: "POST",
    url: 'admin/page.php',
    data: {
        type: 'search',
        date1:'2021/04/15',
        date2:'2021/04/13'
    },
    success: function(data){
        alert(data);     
    }
});

My PHP code

$uname = $_POST["type"];
$date1 = $_POST["date1"];
$date2 = $_POST["date2"];


if ($uname=='search') // condition OK 
{   
    echo $date1;
    echo $date2; 
}   
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
marie_dev
  • 25
  • 5
  • 1
    What is in `$_POST` when you `var_dump()` it? I wonder whether the difference in how you specify the `type` variable to how you specify the dates is causing an issue. Two of them use equal-signs and the other does not. Why the inconsistency? – droopsnoot Apr 15 '21 at 08:22
  • 2
    I'm the`data` object that you POST you should have `date1:'2021/03/03'` not `date=...'` – Tangentially Perpendicular Apr 15 '21 at 08:25
  • @droopsnoot it was a typing error while I was writing this post, I just corrected it – marie_dev Apr 15 '21 at 08:32
  • @ Tangentially Perpendicular yes it was just a typing error while I was writing this post ^^' – marie_dev Apr 15 '21 at 08:33
  • How do you test that? Please describe in detail, what you 're doing. – Marcel Apr 15 '21 at 08:53
  • @marie_dev Problem is that you are using type (keyword) wiithout any quote.Change your data to this - `data: { 'type': 'search', 'date1':'2021/04/15', 'date2':'2021/04/13' },` – Hritik R Apr 15 '21 at 08:54
  • @HritikR that’s not it, quotes would only be required, if the property name was not a valid identifier in JavaScript, see https://stackoverflow.com/a/4348487/1427878 – CBroe Apr 15 '21 at 09:04
  • thank you guys for your answers, it was a syntaxe error,that I was able to see based on your messages ! – marie_dev Apr 15 '21 at 11:47
  • If it's working now, perhaps you could add an answer to show what the problem was, in case it helps anyone else. – droopsnoot Apr 15 '21 at 17:15
  • I changed my data to this --> data: { 'type': 'search', 'date1':'2021/04/15', 'date2':'2021/04/13' } as advised upper, I remplaced = signe by : – marie_dev Apr 16 '21 at 08:03

0 Answers0