# Twitter Bootstrap form Submit & php arrays



## NegativeKelvin (Apr 12, 2012)

I'm working on a minecraft server list as a project because I got bored. It has a submit feature, that submits the IP to a database. I'm trying to do two things, the more important is first.

When the form is submitted, it executes the following PHP Code:


```
<?php
$con=mysqli_connect("localhost","root","","mcservers");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  

$sql="INSERT INTO server (ip, host)
VALUES
('$_POST[server]', '$_POST[host]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
    

mysqli_close($con);
echo 'May take a bit to add to the list! Please be patient!';
echo '<meta http-equiv="Refresh" content="5; url=/mclist">';
?>
```
Currently, it submits it to the database where I manually add it. The list uses a php array, which stores the data like this:


```
<?php
error_reporting(0);

foreach(array('Server', 'Stats') as $file) {
	include sprintf('../mclist/Minecraft/%s.php', $file);
}

$servers = array(
	
	"mc.server1.com",
    "mc.server2.com",
  
);

?>
```
I'm trying to change the system, so instead of it reading off of an array in the php file, it reads from an SQL Database. All I can find in my Sitepoint book isn't very helpful, nor is W3 schools.

*The second thing*
Now, this isn't as important. On this page: http://home.oregongarlic.net/mclist , There is a submit form at the top. Submitting with it will redirect to a page that runs the PHP actions. Is there a jQuery method for doing it so it will remain on the home page and the form will go away after the submit?


----------



## JiminSA (Dec 15, 2011)

2nd point first - I've used jQuery's validate.js and had no probs link here
Not quite sure what you want in your first point - are you wanting to know how to set up and program an SQL database table?
If so Tizzag are pretty easy to understand and get right to it...


----------



## NegativeKelvin (Apr 12, 2012)

I have the database already. I'm trying to get it to take the row from the database and put it into the array.


----------



## JiminSA (Dec 15, 2011)

Now I see.
You could use serialize to set up your data file - php.net have a detailed example on their page
Alternatively you may want to consider a CMS to handle scrutiny directly from the database entry or use phpMyAdmin to check manually


----------



## NegativeKelvin (Apr 12, 2012)

The link for downloading validation js on the page seems to be broken, it just redirects me back to the page.

edit: Nevermind, I got it

edit 2: The post about serialize looks as if it puts things into the database. I'm trying to pull them out. What I'm trying to get it to do is store them from the database into the form of an array. .

edit 3: I just had an idea. I'm going to see if it works.


----------



## JiminSA (Dec 15, 2011)

Well maybe this will give you a little insight? Retrieve your db record(s) and simply translate them thus ...

```
$serverfile = "server.dat";
$server["info"][$info["nextid"]] = array(
"server" => $row["server"],
"host" => $row["host"],
);
$server["nextid"]++;
file_put_contents($serverfile, serialize($server));
```


----------



## NegativeKelvin (Apr 12, 2012)

JiminSA said:


> Well maybe this will give you a little insight? Retrieve your db record(s) and simply translate them thus ...
> 
> ```
> $serverfile = "server.dat";
> ...


I'll try it as soon as I stop getting DDoS'd


----------



## JiminSA (Dec 15, 2011)

> I'll try it as soon as I stop getting DDoS'd


sounds painful - what is being DDoS'd like?


----------



## NegativeKelvin (Apr 12, 2012)

JiminSA said:


> sounds painful - what is being DDoS'd like?


Horrible, I had to check this on my phone... It's slower than dial-up! Much, much, slower.


----------



## NegativeKelvin (Apr 12, 2012)

Back on topic, I just tried the serialize, it works. But it won't let me download validation.js


----------



## JiminSA (Dec 15, 2011)

Try this site they allow a download...


----------



## NegativeKelvin (Apr 12, 2012)

JiminSA said:


> Try this site they allow a download...


The demo works, I'm going to try implementing it it when I get home. Thanks!


----------



## JiminSA (Dec 15, 2011)

Pleasure Kevin
Just one thought though? After converting your db data into a serial file, how do you intend editing/viewing it, with php? 'Cos I can point you to barebones CMS which would do the job if you like (small learning curve) ...
Incidentally, if all you need to do is inspect prospective users and set a permit flag or something you could do all that just using the phpMyAdmin edit facility.
But, I guess you undertook this exercise just to broaden your knowledge base, though ...


----------



## NegativeKelvin (Apr 12, 2012)

It was for listing the servers from the SQL database into the page using Redwallhp's server status system, which gets the servers from an array which is inside of the PHP file. The problem with that, is, I'm trying to setup the list so people can submit servers, it checks to see that the server is valid. If the server address is valid, it will add to the list.

Edit: I'm now figuring out what I'm doing, because I'm not even using the right code some how xD


----------

