0

I have a set of variables that I am sending to a php file using ajax. Instead of sending all of them as individual variables, is there a way to send them as 1 string and then break up that string in the php page?

Main page.

<script type="text/javascript">
        $(document).ready(function(argument) {
            $('#save').click(function(){
                // Get edit field value
                $edit1a = $('#edit1a').html();
                $edit2a = $('#edit2a').html();
                $edit3a = $('#edit3a').html();
                $edit4a = $('#edit4a').html();
                $edit5a = $('#edit5a').html();
                $edit6a = $('#edit6a').html();
                $edit7a = $('#edit7a').html();
                $edit8a = $('#edit8a').html();
                $edit9a = $('#edit9a').html();
                $edit10a = $('#edit10a').html();
                $edit11a = $('#edit11a').html();
                $.ajax({
                    url: 'get.php',
                    type: 'post',
                    data: {edit1: $edit1a, edit2: $edit2a, edit3: $edit3a, edit4: $edit4a, edit5: $edit5a, edit6: $edit6a, edit7: $edit7a, edit8: $edit8a, edit9: $edit9a, edit10: $edit10a, edit11: $edit11a,},
                    datatype: 'html',
                    success: function(rsp){
                            alert(rsp);
                        }
                });
            });
        });
    </script>

php page.

$edit1 = @$_POST['edit1'];
$edit2 = @$_POST['edit2'];
$edit3 = @$_POST['edit3'];
$edit4 = @$_POST['edit4'];
$edit5 = @$_POST['edit5'];
$edit6 = @$_POST['edit6'];
$edit7 = @$_POST['edit7'];
$edit8 = @$_POST['edit8'];
$edit9 = @$_POST['edit9'];
$edit10 = @$_POST['edit10'];
$edit11 = @$_POST['edit11'];



$insert =mysql_query('INSERT INTO pages (edit1, edit2, 
edit3, edit4, edit5, edit6, edit7,edit8, edit9, edit10, 
edit11)   VALUES("'.$edit1.','.$edit2.','.$edit3.','.$edit4.','.$edit5.','.$edit6.',
'.$edit7.','.$edit8.','.$    edit9.','.$edit10.','.$edit11.'")')
or die ("could not insert new Data:" .mysql_error()); 
amazingacademy
  • 143
  • 1
  • 9
  • Perhaps `serialize()`? http://api.jquery.com/serialize/ – Jay Blanchard Sep 25 '14 at 21:26
  • Lovely [sql injection attack](http://bobby-tables.com) vulnerabilities. Enjoy having your server pwn3d. – Marc B Sep 25 '14 at 21:26
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 25 '14 at 21:26
  • and another person trying to up their comments, maybe I just have a simple sql file for now to test the function is all... – amazingacademy Sep 25 '14 at 21:27
  • 1
    glueing them all together in to a single string only to break them up again, i dont see the point in that –  Sep 25 '14 at 21:30
  • Referring to marc, Thanks for the artiicle Jay and yes I am very familiar of the security risks. I just have a test page coded this way to check the functionality. It is all local atm and when I go live it will use a good bit of laravel for all of the handling – amazingacademy Sep 25 '14 at 21:32
  • Mostly it's the way it is stored in my db, instead of a different column for every input, I would just have 1 column with all of them. – amazingacademy Sep 25 '14 at 21:33

1 Answers1

0

You can do something like this:

            $edit1a = $('#edit1a').html();
            $edit2a = $('#edit2a').html();
            var sepStr = '<SEPARATOR>'; //reserve whatever character or string you want
            var combined1a2a = $edit1a + sepStr + $edit2a;
            $.ajax({
                url: 'get.php',
                type: 'post',
                data: {combined: combined1a2a},
                datatype: 'html',
                success: function(rsp){
                        alert(rsp);
                    }
            });

And in PHP:

$values = explode('<SEPARATOR>', POST['combined']);
//access like $values[0], $values[1], etc.

(I would stop using $ in front of javascript var names if I were you. You will start getting confused what's javascript and what's PHP by doing that.)

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • cant you use some jquery selector to get all #edit* in to an array? –  Sep 25 '14 at 21:36
  • @Dagon, Probably, but to send it all as one string you need a string, not an array. I don't get the point of doing this to begin with, but this is one way it could be done. – developerwjk Sep 25 '14 at 21:37
  • I see what you are talking about. It was mostly to reduct columns in the database, but I think i'm just going to go the route that I have it as. Seems like a waste of time for something that works descent – amazingacademy Sep 25 '14 at 21:40