• Let me preface this by saying that I know very little about PHP and HTML. My experience lies with C++. I’m learning all of this as I go, so I appreciate any help that I can get.

    I’m executing a PHP code inside my WordPress page (not used as a blog) that basically creates a MySQL table using the user’s login ID, then on the same page I execute an html input form where the user may input some information which is then sent into a separate PHP file, that uses $_POST to insert the information into the MySQL table. The reason I’m doing it this way is because these tables will be deleted on a weekly basis, so they need to be recreated if they do not exist.

    The first two steps which are coded inside my WordPress editor (not visual editor) are as follows (I shortened the code here to only pertinent sections):

    <?php
    
    //Get Current User Login
    global $current_user;
    $current_user = wp_get_current_user();
    $ulog = $current_user->user_login;
    $tablename_db = "db_".$ulog;
    
    mysql_select_db("data_base_name_here");
    
    //Create Table If It Does Not Exist
    $sql = "CREATE TABLE IF NOT EXISTS $tablename_db (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    db_num int,
    db_amnt int
    );";
    
    mysql_query($sql); ?>
    
    <html>
    <body>
    
    <form action="DirectBill.php" method="post">
    <fieldset>
    <legend>Direct Bill Information:</legend>
    DB #: <input type="int" name="db_num_1" /> DB Amount: <input type="int" name="db_amnt_1" /> </br>
    <!--Above table entries is repeated 10 more times-->
    <strong><!--<input type="hidden" name="user_name" value="user_login_ID>--></strong>
    </fieldset>
    <input type="submit" />
    </form>
    
    </body>
    </html>

    The issue that I’m having is that I need to send the user’s login ID as a hidden variable (the comment line that is in bold in the html code) into the ‘DirectBill.php’ page, so that I can use it to process the information, but I can’t seem to figure out how to do that. I also tried including the WordPress API in the ‘DirectBill.php’ page, so that I can just recollect the user ID there, but that didn’t seem to work either.

    Does anyone have any suggestions as to how I can accomplish this? Thank you all in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Kimusubi

    (@kimusubi)

    I sort of solved my own problem. I found two methods of doing it. In the first method, I executed a php code inside the html form and echoed the variable $ulog, like so:

    <input type="hidden" name="user_name" value="<?php echo $ulog; ?>">

    Then I used that in my other php page, ‘DirectBill.php’, and executed the remainder of the code.

    In the second method, I just used $PHP_SELF action in the form and used an if statement to execute the code. This way I kept everything within the editor. It looks a lot neater, but I’m not sure if it’s better or not. This is what the code looks like for the second method:

    <?php
    
    if(isset($_POST['submit']))
    {
    	//Get Current User Login
    	global $current_user;
    	$current_user = wp_get_current_user();
    	$ulog = $current_user->user_login;
    	$tablename_db = "db_".$ulog;
    
    	//Check To See If User Has Already Created Table
    	$sql = "CREATE TABLE IF NOT EXISTS $tablename_db (
    	ID int NOT NULL AUTO_INCREMENT,
    	PRIMARY KEY(ID),
    	db_num int,
    	db_amnt int
    	);";
    
    	mysql_query($sql);
    
    	$sql2="INSERT INTO $tablename_db (db_num, db_amnt)
    	VALUES
    	('$_POST[db_num_1]','$_POST[db_amnt_1]')";
    
    	if (!mysql_query($sql2))
    	{
    		die('Error: ' . mysql_error());
    	}
    	echo '<strong>', "Your DBs have been submitted and will be added to your account upon approval.", '</strong>';
    
    }
    ?>
    
    <html>
    <body>
    
    Please fill in your DB information in the form below and press submit. Leave all unused fields blank.
    
    <form method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
    <fieldset>
    <legend>Direct Bill Information:</legend>
    DB #: <input type="int" name="db_num_1" /> DB Amount: <input type="int" name="db_amnt_1" /> </br>
    DB #: <input type="int" name="db_num_2" /> DB Amount: <input type="int" name="db_amnt_2" /> </br>
    DB #: <input type="int" name="db_num_3" /> DB Amount: <input type="int" name="db_amnt_3" /> </br>
    DB #: <input type="int" name="db_num_4" /> DB Amount: <input type="int" name="db_amnt_4" /> </br>
    DB #: <input type="int" name="db_num_5" /> DB Amount: <input type="int" name="db_amnt_5" /> </br>
    DB #: <input type="int" name="db_num_6" /> DB Amount: <input type="int" name="db_amnt_6" /> </br>
    DB #: <input type="int" name="db_num_7" /> DB Amount: <input type="int" name="db_amnt_7" /> </br>
    DB #: <input type="int" name="db_num_8" /> DB Amount: <input type="int" name="db_amnt_8" /> </br>
    DB #: <input type="int" name="db_num_9" /> DB Amount: <input type="int" name="db_amnt_9" /> </br>
    DB #: <input type="int" name="db_num_10" /> DB Amount: <input type="int" name="db_amnt_10" />
    </fieldset>
    <input type="submit" value="submit" name="submit" />
    </form>
    
    </body>
    </html>

    Does anyone know which method is more efficient?

    Moderator bcworkz

    (@bcworkz)

    Not enough difference to make a difference I’d say. Do which ever you think will make more sense to someone else maintaining the code two years later.

    Be sure to sanitize the input to avoid SQL injection attacks, you probably just took that part out in your example, but it’s worth mentioning.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Passing PHP Variable Into HTML Form On WordPress Page’ is closed to new replies.