Note that there are some explanatory texts on larger screens.

plurals
  1. POReceive jQuery ajax posted value in PHP
    text
    copied!<p>I have a form where the user inputs data and a table that shows all the rows in the database. Now when the user clicks an "edit"-link next to one of the rows I want to post the ID to the PHP file and load the data in the row into the form. </p> <p>This is how I post the ID (and it works)</p> <pre><code>jQuery('a.edit').on('mousedown', function(){ var id = jQuery(this).attr('data-title'); jQuery.ajax({ url: document.location, data: {id: id} }); }) </code></pre> <p>This is how I tried to receive the posted ID (doesn't work)</p> <pre><code>if( isset( $_GET['id'] ) ){ $id = $_GET['id']; load_data($id); } </code></pre> <p>I guess I need to refresh the page? How can I do that? Can I also just refresh the form?</p> <p><strong>EDIT:</strong> I still think it's a reload/refresh issue. The value is being posted, but my PHP doesn't recognize because it is not reloaded. I've seen people doing this with jQuery instead of reloading the whole page, but I fail to recreate this. Maybe it is also worth mentioning that theses files are part of a Wordpress Plugin..</p> <p>So this is the whole code (my plugin is called "spielerportraits")</p> <p>spielerportraits-admin.php</p> <pre><code>&lt;?php function print_data(){ global $wpdb; $datatable = $wpdb-&gt;get_results( 'SELECT * FROM ' . $wpdb-&gt;prefix . 'spielerportraits' ); ?&gt; &lt;table id="spieler-output"&gt; &lt;tr&gt; &lt;td&gt;Name&lt;/td&gt; &lt;td&gt;Geb.datum&lt;/td&gt; &lt;/tr&gt; &lt;?php foreach( $datatable as $data ) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $data-&gt;name; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $data-&gt;gebdatum; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="" class="edit" data-title="&lt;?php echo $data-&gt;id;?&gt;"&gt;bearbeiten&lt;/a&gt;&lt;/td&gt; // The edit-link for jQuery &lt;/tr&gt; &lt;?php } ?&gt; &lt;/table&gt; &lt;?php } if(isset($_GET['id'])){ global $wpdb; $loaddata = $wpdb-&gt;get_row( 'SELECT * FROM '.$wpdb-&gt;prefix.'spielerportraits WHERE id = '.$_GET['id'] ); } ?&gt; &lt;form enctype="multipart/form-data" name="spieler-form" action="" method="post"&gt; &lt;table id="spieler-input"&gt; &lt;tr&gt; &lt;td&gt;Name: &lt;/td&gt; &lt;td&gt;&lt;input type="text" name="name" value="&lt;?php if( isset($loaddata) ){echo $loaddata-&gt;name;}?&gt;" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Geburtsdatum: &lt;/td&gt; &lt;td&gt;&lt;input type="text" name="gebdatum" value="&lt;?php if( isset($loaddata) ){echo $loaddata-&gt;gebdatum;}?&gt;" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="submit"&gt; &lt;input type="submit" name="submit" value="Hinzufügen" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;?php print_data(); ?&gt; </code></pre> <p>The JS</p> <pre><code>jQuery('a.edit').on('mousedown', function(){ var id = jQuery(this).attr('data-title'); jQuery.ajax({ url: document.location, data: {id: id}, success: function(data){ alert(data['id']); // This does show, but gives me 'undefined' } }); }); </code></pre> <p>And this is how the plugin is initialised in Wordpress (spielerportraits.php):</p> <pre><code>&lt;?php add_action( 'admin_enqueue_scripts', 'spielerportraits_style' ); add_action('admin_menu', 'spielerportraits_custom_menu_page'); function spielerportraits_custom_menu_page() { $page = add_menu_page( 'Spielerportraits', 'Spielerportraits', 'manage_options', 'spielerportraits/spielerportraits-admin.php', '', '' ), 33 ); } function spielerportraits_style() { wp_enqueue_script( 'spielerportraits-admin-script', plugins_url('/script.js', __FILE__) ); } global $jal_db_version; $jal_db_version = "1.0"; function jal_install() { global $wpdb; global $jal_db_version; $table_name = $wpdb-&gt;prefix . 'spielerportraits'; $sql = "CREATE TABLE $table_name ( id int NOT NULL AUTO_INCREMENT, name tinytext NOT NULL, gebdatum date', PRIMARY KEY id (id) );"; if( $wpdb-&gt;get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) { require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( "jal_db_version", $jal_db_version ); } } register_activation_hook( __FILE__, 'jal_install' ); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload