Jump to content

New to php need a little help.


DwayneB

Recommended Posts

Ok here it goes guys let me explain what I want to do.

 

I am writing a PHP script built into a HTML file.  The page will have a series of tables.  The fist table will have be were the user inputs the required information to connect to there mysql database. at the bottom of the table will be a button with the onclick action set to call my php script to connect to the data base.  Once connected I want data saying connected successfully or the error messges put into the table data fields marked in red below.  Button is in blue. do I put the php script with in the <td> I want the respose to?  Making sure it echo's the html tags needed?

 

<tr><td align="right">Database host: </td><td> <input type="text" name="host"/></td></tr>

<tr><td align="right">Database name:</td><td> <input type="text" name="dbname"/></tr>

<tr><td align="right">Username:</td><td><input type="text" name="username"/></td></tr>


<tr><td align="right">Password:</td><td><input type="password" name="password"/></td></tr>

<tr><td>out put from the php script that connects to the database and makes sure its connected to the right database</TD</tr>

<tr><td><button ---- to call the php function to connect to the data base/></td></tr>
 

 

This is the code I have now but I'm not sure if this will work.  I'm not sure how to reference the data in the input fields, to the peramiters passed to the function.  Can someone shed some light??

 

Thanks

 

<div id="database">
        <table border = "1" align="center" cellpadding="10">
            <tr>
            <td align="right">Zen-Cart Database Name:</td><td><input type="text" name="dname" /></td><td align="right" >Database Table Prefix(no spaces):</td><td><input type="text" name="prefix"/></td>
            </tr>
            <tr>
            <td align="Right">Database Username:</td>   <td><input type="text" name="Uname"/></td>  <td align="right">Password:</td> <td><input type="password" name="pass"/></td>
            </tr>
            <tr>
            <td align="right">Database host:</td><td><input type="text" name="host"/></td>
            <tr>
            <?php
                    Function dbconect($host,$dbname,$Un,$pass,)
                    {
                        mysql_connect($host,$Un,$pass) or die (my_err());
                        echo("<td>Connected to your mysql database!</td>");
                       
                        mysql_select_db($dbname) or die (mysql_error());
                        echo('<td>Connected to My Sql database name ' . $dbname . '</td>); 
                    }
                 ?>
              </tr>                
            <tr><td>   </td><td colspan="2"><button type="button onclick="dbconnect(host,dname,Uname,pass)">Test Connection</button></td></tr>
        </table>
    </div>

 

 

 

 

Link to comment
Share on other sites

Hi,

 

Im not quite sure I get exactly what you are trying to achieve or how you are planning on going about it.

 

How do you plan on submitting the data from the form? Do you want to do this using AJAX so the page itself does not change? I cannot see any <form submit="page.php"> or anything like this, so can you explain how you see this working when its complete?

 

Andrew

Link to comment
Share on other sites

PHP is a server side language, so it doesn't have access to the browser's Document Object Model (DOM), so it can't detect things like mouse movements or clicks.  Further, PHP does not (usually) run as a continual process.  By the time output is rendered to the screen, the PHP script is done executing.

 

What you need to use is AJAX, which is a fancy term meaning "Some JavaScript that will send a request to a server side script, grab the result, and then display it."

 

I suggest you break things up into two files:

 

1. Your PHP script without any HTML in it.  This will just contain your db code that will send results back to...

2. An HTML file, which will contain both your form and the JavaScript needed to handle the AJAX part of things

 

Why do it this way?  Because it's good design.  One of the big mistakes newbies make is cramming everything into one file because that's how a million shitty online tutorials do it.  In actuality, that's completely the wrong way to do it, especially when you're jumping between PHP and HTML multiple times in the same file.  That 'feature' of PHP is only really useful in a certain context.  By breaking things up, you make your code easier to write, easier to read, easier to edit, easier to maintain, and easier to debug.

 

For AJAX itself, I recommend you look into using jQuery (which is an awesome JavaScript framework), specifically its $.post() function.  That will do what you want to do if you tie it into a $.click() or $.submit().

 

Beyond that, I'm not familiar with Zen Cart, so I'm not sure if the two file setup would work for your entire app.  You should check its docs to see how it recommends separating its display code from its logic.

Link to comment
Share on other sites

Thanks guys,  Your right about the AJAX.  I am not sure what I was doing last night.  I made things way more complicated then it has to.

 

What I am trying to do is organize products into better categories then they already are.  Problem is there is 7000+ products.  I have a plug in that will export a csv file of all products, details including location of the img for the product.  I want my page to read that file, create a table showing product ID, Name,Description and the image (not the name but the image) as well as a check box for 50 products at a time.  I can then place a check in each product that I choose, at bottom of the 50 enter a number representing the ID of the category, hit submit and a mysql or mysqli or PDO commeand would update the database.  

 

The hole deal above was; I wanted others to be able to use this, So they should have been able to enter there database information and the button would have been for them to test the connection to the database.  I have a better way of accomplishing this task now though!!

 

Thanks everyone. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.