0

i am facing a problem that after i created the jQuery post, i was able to receive all the data but as a one peace, so when i began rephrasing them i succeed until the final part which was the inserting into the database, where inside the for loop and if loop i am getting the value but when i wanted to start inserting them into the database i am getting null values, below is the for loop and if loop

if ($action == "insert")
{
    $fields = explode("&",$data);

    foreach($fields as $field)
    {
        $field_key_value = explode("=",$field);
        $key = urldecode($field_key_value[0]);
        $value = urldecode($field_key_value[1]);

        $id = $row['id'];

        $date1 = date("d/n/Y");

        foreach ($cart->get_contents() as $item)
        {
            $item_id    = $item['id'];
            $item_name  = $item['name'];
            $item_price = $item['price'];
            $item_qty   = $item['qty'];
            $item_ids       = explode("-",$item_id);

            for($i = 0; $i < count($item_ids); $i++)
            {
                $item_idn = join("",$item_ids);
            }

            if($key == $item_id."id")
            {
                $ids = $value;
                echo $ids."\r\n";
            }
            elseif($key == "Small".$item_idn)
            {
                $small= $value;
                echo $small."\r\n";
            }
            elseif($key == "large".$item_idn)
            {
                $large= $value;
                echo $large."\r\n";
            }
            elseif($key == "medium".$item_idn)
            {
                $medium= $value;
                echo $medium."\r\n";
            }
            elseif($key == "xlarge".$item_idn)
            {
                $xlarge= $value;
                echo $xlarge."\r\n";
            }
            elseif($key == "qty".$item_idn)
            {
                $qty = $value;
                echo $qty."\r\n";
            }
            elseif($key == "Total".$item_idn)
            {
                $subtotal = $value;
                echo $subtotal."\r\n";
            }
            elseif($key == "finaltotal")
            {
                $finaltotal = $value.",";
                $final = explode(",",$finaltotal);

                for($i = 0; $i < count($final); $i++)
                {
                    $totalf = $final[$i];
                    break 3;
                }
            }
        }
    }
}
6
  • 1
    What is the value of $value and why do you append a , at it's end? Commented Oct 8, 2010 at 13:00
  • what variable are you trying to print? Commented Oct 8, 2010 at 13:00
  • please provide more information and code (and format it properly). I don't get what the exact problem is from your description. Commented Oct 8, 2010 at 13:01
  • because $value is an array contains a mixed data of all my form, so i wanted to split all these data to its respective variable so that i can be easier to be inserted into the database Commented Oct 8, 2010 at 13:02
  • 2
    You cannot explode an array even more you cannot append a string to an array. Commented Oct 8, 2010 at 13:03

3 Answers 3

1

From jQuery docs:

The .serialize() method creates a text string in standard URL-encoded notation

So on the PHP side you'll get a similar string like this:

a=1&b=2&c=3&d=4&e=5

There's no need to explode &'s (or any other hocus-pocus) you can easily access your submitted variables like:

$a = $_POST['a']; //1

Of course when you submit your data via $_GET, you need to use $_GET.

Sign up to request clarification or add additional context in comments.

2 Comments

hey there @fabrik when i tested your method i keep on getting null
What you get when you do print_r($_POST) on the PHP side?
0

It appears as if you have a number of issues. If your jQuery is posting to your PHP script you can get all your keys/values through the $_POST array. You could get $finaltotal like this $finaltotal = $_POST['finaltotal']. If your jQuery is not POSTing, but sending them as a GET request, you can get the same value like so $finaltotal = $_GET['finaltotal'].

Also, regarding your big if/elseif block of code, I would recommend using a switch statement instead if you are going to keep your code as is.

switch($key)
{
    case 'finaltotal':
        //do stuff
        break;
    default:
        //do default
        break;
}

Comments

0

First of all, you either can use $_GET/$_POST for the submited data, as fabrik pointed out, or (if for some reason you really only have your data in $data) can use the built in function parse_str.

parse_str($data, $fields);
foreach($fields as $key => $field) {
  foreach ($cart->get_contents() as $item) {
    if($key == ...) {
      ...
    }
  }
}

And if I correctly understand what you are doing here, you need to move the break 3; out of your for loop:

elseif($key == "finaltotal") {
  $finaltotal = $value.",";
  $final = explode(",",$finaltotal);

  for($i = 0; $i < count($final); $i++){
    $totalf = $final[$i];
  }
  break 3;
}

1 Comment

Hey there @slosd when i am doing here is that i am stop the if loop from creating duplication, without the break i keep on getting 100.00100.00100.00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.