'Connect to mysql database using function.php and echo in the same function.php
I created a form to allow me to enter the data of a company in the cart page from which I then print a pdf in which the company data is shown.
The form I created allows me to save the companies created through the form on the database in a custom table (aziende).
I inserted the module and the script that makes the connection in functions.php, here I also inserted a search module from which I want to recall the companies previously saved (ragione) and print the data directly in the module so as to be able to show the data in the pdf and possibly modify the values and in the future maybe insert them in the native wordpress table of the users. Using external pages I can enter, edit and search for companies, but I cannot print the search results directly in the form I use to create new records and to print pdfs
This is my function in mytheme/function.php
function st_contact_form(){
ob_start();
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
global $wpdb;
$ricerca = $_GET["s"];
$results = $wpdb->get_results( "SELECT * FROM aziende WHERE ragione ='$ricerca'");
foreach ( $results as $risultato)
{
echo
'';
}
?>
<form role="search" method="get" action="" >
<div><label class="screen-reader-text" for="s">SEARCH</label>
<input type="text" onkeyup="this.value = this.value.toUpperCase();" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="cerca azienda" />
</div>
</form>
<div class="contact_form-wrapper popup-contact-form" ">
<label>Intestazione:</label><br>
<form target="_blank" action="http://localhost/gestionale2/wp-content/themes/fashiro/process.php" method="post" name="contact_form" id="contact_form" autocomplete="off">
<table width="100%" border="1">
<tbody>
<tr>
<td>ID</td>
<td>RAGIONE SOCIALE</td>
<td>INDIRIZZO</td>
<td>CAP</td>
<td>CITTA'</td>
</tr>
<tr>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase(); "value="<?php echo "$risultato->azienda_id" ?>" name="azienda_id"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase(); "placeholder="Ragione sociale/cliente" value="<?php echo "$risultato->ragione" ?>" name="ragione" placeholder="Ragione sociale/cliente"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->indirizzo" ?>" name="indirizzo" placeholder="Indirizzo"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->cap" ?>" name="cap" placeholder="CAP"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->citta" ?>" name="citta" placeholder="citta"></td>
</tr>
<tr>
<td>PROVINCIA</td>
<td>PARTITA IVA</td>
<td>COD. FISCALE</td>
<td>COD. DESTINATARIO</td>
<td>PAGAMENTO</td>
</tr>
<tr>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->cap" ?>" name="provincia" placeholder="Provincia"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->piva" ?>" name="piva" placeholder="partita iva o cf"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" value="<?php echo "$risultato->cfiscale" ?>" name="cfiscale" placeholder="codice fiscale"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" name="codest" value="<?php echo "$risultato->codest" ?> "placeholder="Codice destinatario"></td>
<td><input type="text" onkeyup="this.value = this.value.toUpperCase();" name="pagamento" value="<?php echo "$risultato->pagamento" ?> " placeholder="Metodo di pagamento"></td>
</tr>
<tr>
<td>TRASPORTO</td>
<td>NOTE</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><select type="select" name="trasporti"><br>
<option selected disabled>Seleziona spese di trasporto</option>
<option value="PORTO FRANCO">PORTO FRANCO</option>
<option value="PORTO ASSEGNATO">PORTO ASSEGNATO</option>
</select></td>
<td colspan="4"><textarea cols="5" rows="4" name="comments" value="" id="para1"><?php echo "$risultato->comments" ?></textarea></td>
</tr>
<tr>
</tbody>
</table>
<hr />
<div class="form-group ">
<input type="submit" name="send" value="SAVE" class="btn_st btn_maroon form_submit_btn">
<input type="submit"formaction="http://localhost/gestionale2/wp-content/themes/fashiro/modificaziende.php" name="send" value="UPDATE" class="btn_st btn_maroon form_submit_btn">
<input type="submit" name="button" formaction="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) ); ?>" value="PRINT PDF"/>
</div>
</form>
<?php
return ob_get_clean();
}
add_shortcode( 'contact_form', 'st_contact_form' );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
